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.
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:
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.