[英]how to create a program that inputs a sentence and then displays each word of the sentence on a separate line in python
how to create a program that inputs a sentence and then displays each word of the sentence on a separate line如何创建一个程序,输入一个句子,然后在单独的一行上显示句子的每个单词
I have tried using the split function but instead it displays the words on the same line, in a list.我曾尝试使用 split function 但它在同一行的列表中显示单词。
here is my code:这是我的代码:
sentence = input("Enter a sentence:")
print(sentence.split(" "))
You could do:你可以这样做:
sentence = input("Enter a sentence:")
for item in sentence.split(" "):
print(item)
You can use the sep keyword argument:您可以使用 sep 关键字参数:
sentence = input("Enter a sentence:")
print(*sentence.split(" "), sep='\n')
Outputs:输出:
Enter a sentence: The quick brown fox jumps over the lazy dog
The
quick
brown
fox
jumps
over
the
lazy
dog
split creates a list.拆分创建一个列表。 You might want to iterate over that list and print the word one by one
您可能想遍历该列表并一个一个地打印单词
sentence = input("Enter a sentence:")
words = sentence.split(" ")
for word in words:
print(word)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.