[英]How to pad a string with 'X' to a minimum length in Python?
最近,在我的GCSE計算機科學課程中,我們被要求編寫一個程序,該程序將一個人的名字和姓氏放在一個字符串中,然后接受名字的首字母和姓氏的前四個字母並打印出來。 ,如果用戶的姓氏長度不超過一個字符,它將用字母'X'代替空格。
在考試中,我們不允許訪問互聯網,因此我在不使用互聯網的情況下開發了該程序,但是現在我想知道是否有更簡便的方法來編寫我編寫的程序。
while True:
fullname = input("\nPlease type in your firstname and your surname: ")
space = fullname.index(" ")
length = len(fullname)
x = "X"
endsur = length - 1
initialfirst = fullname[0]
fullsurname = endsur - space
if fullsurname >= 4:
startsur = space + 1
secondsur = space + 2
thirdsur = space + 3
fourthsur = space+ 4
surname = fullname[startsur] + fullname[secondsur] + fullname[thirdsur] + fullname[fourthsur]
print(initialfirst,surname)
again = input("\nWould you like to go again? (Y/N)")
if again == "y":
continue
if again == "n":
print("Goodbye!")
quit()
if fullsurname == 3:
startsur = space + 1
secondsur = space + 2
thirdsur = space + 3
surname = fullname[startsur] + fullname[secondsur] + fullname[thirdsur] + x
print(initialfirst,surname)
again = input("\nWould you like to go again? (Y/N)")
if again == "y":
continue
if again == "n":
print("Goodbye!")
quit()
if fullsurname == 2:
startsur = space + 1
secondsur = space + 2
surname = fullname[startsur] + fullname[secondsur] + x + x
if again == "y":
continue
if again == "n":
print("Goodbye!")
quit()
if fullsurname == 1:
startsur = space + 1
surname = fullname[startsur] + x + x + x
if again == "y":
continue
if again == "n":
print("Goodbye!")
quit()
if fullsurname <= 0:
print("\nIt looks like you've not put in your surname. Please make sure to use a space between your firstname and your surname.")
while True:
name = input('What is your name? ')
name = name.split(' ')
first_name = name[0]
last_name = name[1]
if len(last_name) < 4:
last_name += 'x'*(4-len(last_name))
together = first_name[0] + last_name[0:4]
#You can add your error checks here
print(together)
cont = input('Would you like to go again? ')
if cont == 'y':
pass
else:
break
這段代碼是@Wright的答案的附加內容:提示:避免創建不必要的變量來進一步壓縮代碼,並避免命名空間泛濫
while 1:
first_name, last_name = input('What is your name? ').split(' ')
if len(last_name) < 4:
last_name += 'x'*(4-len(last_name))
together = first_name[0] + last_name[0:4]
print(together)
if input('Would you like to go again? ') != 'y':
break
print("Enter your firstname and your surname (ex. Christopher Brown)")
while True:
name = input('> ').split()
if name == ['q']:
exit()
elif len(name) < 2:
print("Invalid input")
continue
print(name[0][0],name[1][:4 if len(name[1]) >= 4 else len(name[1])]+'X'*(4-len(name[1])))
我為您大大縮短了代碼,我將獲取名稱的所有輸入移到一行中的一個變量中,然后進行了檢查並從一行中打印了所有內容,不客氣!!!
確保姓氏具有足夠字符的最簡單方法是首先添加'XXXX'
填充。
fullname = input(…)
firstname, surname_x = (fullname + "XXXX").split()
print(firstname[0], surname_x[:4])
您也可以為此使用Python的字符串格式 。
while True:
name = input('What is your name? ')
print('{:.1}{:x<4.4}'.format(*name.split(' ', 1)))
if input('Would you like to go again? ') != 'y':
break
.1
和.4
表示名字和第二名將被截斷,而x<4
表示應該用x
s填充第二名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.