繁体   English   中英

输出用户输入的每个单词的第一个字母

[英]Output first letter of each word of user input

我有这个代码

#Ask for word
w = input("Type in a word to create acronym with spaces between the words:")

#Seperate the words to create acronym
s = w.split(" ")
letter = s[0]

#print answer 
print(s.upper(letter))

而且我知道我需要一个for循环遍历单词以获取每个单词的第一个字母,但我无法弄清楚如何做到这一点我尝试了很多不同的类型,但我不断收到错误。

尝试这个。 它打印每个单词的第一个字母的连接版本。

w = input("Type in a word to create acronym with spaces between the words:")
print(''.join([e[0] for e in w.split()]).upper())

尝试这个

w = input("Type a phrase with a space between the words:")
w_up_split = w.upper().split()
acronym = ""

for i in w_up_split:
    acronym += (i[0])
print(acronym)
for word in w.split(" "):
    first_letter = word[0]
    print(first_letter.upper())

在您给出的代码中,您将获取列表列表中的第一个单词。

s = w.split(" ")
letter = s[0]

如果有人输入“你好,你好吗”这个s就等于

s == ["Hi"]["how"]["are"]["you"]

然后字母将等于s的第一个索引,即[“Hi”]

你想通过每个单词并拿走每个字母

acronym = []
for x in s:
    acronym.append(x[0])

会得到你想要的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM