打印全名python的第一个字母



你好,我正在尝试创建一个程序,输入并打印出首字母大写,但我不明白为什么我的程序只打印字符串分割后列表最后一项的第一个字母

这是我的代码:

full_name = input("Please enter your full name: ")
name = full_name.split()
for item in name:
new_name = item[0].upper()

print(new_name)

您可以创建一个新的空变量,如initials,并添加第一个字母

full_name = input("full name: ")
name = full_name.split()
initials = ""
for item in name:
initials += item[0].upper()
print(initials)

我想这对你有帮助:

# get the full name from the user
full_name = input("Enter your full name: ")
# split the name into a list of words
name_list = full_name.split()
# loop through the list of words

for i in range(len(name_list)):
# get the current word
word = name_list[i]
# uppercase the first letter of the word
word = word[0].upper() + word[1:]
# replace the word in the list with the new word
name_list[i] = word

# join the list of words into a string
full_name = " ".join(name_list)
# print the full name
print(full_name)

最新更新