繁体   English   中英

简单的首字母缩略词程序python有问题

[英]Trouble with simple acronym program python

我正在尝试制作一个首字母缩略词程序,用户可以在其中每行输入 1 个单词,在输入空格后,程序将输出由每个单词的第一个字母创建的单词。 它在我不想要的输出之后打印一个空格。 关于如何删除它的任何想法? 更多信息如下:

例如

User input:
Word: Hello
Word: World

Desired output:
Hello World <-- whitespace here that I don't want
-- HW

My Current Code that works:

words = []
word = input('Word: ')
while word:
  words.append(word)
  word = input('Word: ') #Word input

for word in words:

   print(word, end=" ") #displays all words on one line 

  # Printing a white space ^

# Wanting to print this code on a new line 
first_letters = ''.join([word[0] for word in words])
new=first_letters.upper()
print("\n-- " + new)

您快到了。 将你所拥有的与numprint结合起来:

for word in words:
    print(word[:1], end="")  # Display all first letters without spaces.

就这个?

words = ['Hello','World']
print(' '.join(w for w in words))
print('-- '+''.join(w[0] for w in words))

给定字符串数组words 有几种方法可以获取单词的首字母。 其中很少有人在下面。

第一种方法

first_letters = ''
for word in words:
  first_letters += word[0]
print(first_letters) 

第二种方法

first_letters = ''.join([word[0] for word in words])
print(first_letters)

这段代码做你想要的一切

words = []
word = input('Word: ')
while word:
  words.append(word)
  word = input('Word: ') #Word input


print(' '.join(words))


first_letters = ''.join([word[0] for word in words])
new=first_letters.upper()
print("-- " + new)

暂无
暂无

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

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