python关于字符串问题

img


题目的意思就是把输出的姓名控制到五位数,其中名只取第一个字母并改为小写,姓改为大写。如果输出不足五位数,那后面加上星号(※)
这个是我写的代码,只能来计算这个

img

a = input("Please enter your first name: ")
b = input("Please enter your surname: ")
full_name = a +b
space_index = len(a)-1
first_name = full_name[:space_index]
last_name = full_name[space_index+1:space_index+5].upper()
first_letter = first_name[0].lower()
initialled_name = first_letter+last_name
print(initialled_name)
但是这个应该怎么去加代码呢

img

a = input("Please enter your first name: ")
b = input("Please enter your surname: ")
s=a.lower()[0]+b.upper()
if len(s)>=5:
    s=s[0:5]
else:
    s=s.ljust(5,'*')
print('Your username is: %s'%s)

截取名第一个字母转为小写,拼接姓

a = input("Please enter your first name: ")
b = input("Please enter your surname: ")
initialled_name = a[0].lower()+b.upper()
if 5-len(initialled_name)>0:
    print('Your username is:',initialled_name+str('*').zfill(5-len(initialled_name)))
else:
    print('Your username is:',initialled_name[:5])
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632