Python Forum
Put each word in a quote in a new line in printout - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Put each word in a quote in a new line in printout (/thread-8300.html)



Put each word in a quote in a new line in printout - bjorntobias - Feb-13-2018

Hi guys,

I'm trying to take an input value, a quote, and place each word of that quote into a new line when printed. I'm not really succesful with that. Rather than placing each work on a new line it just appends each word to the new line.


# [ ] Print each word in the quote on a new line  
quote = "they stumble who run fast"
​
start = 0
space_index = quote.find(" ")
​
while space_index != -1:
    print(quote[:space_index])
    space_index = quote.find(" ", space_index + 1)
    
which results in

they
they stumble
they stumble who
they stumble who run
they stumble who run fast

But I would like it to print,

they
stumble
who
run
fast

Any tips and trix for solving this?

Thanks a lot,
Tobias


RE: Put each word in a quote in a new line in printout - Larz60+ - Feb-13-2018

quote = "they stumble who run fast"
print([x for x in quote.split()])
Sorry, I misread the task.


RE: Put each word in a quote in a new line in printout - Larz60+ - Feb-13-2018

quote = "they stumble who run fast"
x = quote.split()
newitem = ''
for item in x:
    newitem += '{} '.format(item)
    print('{}'.format(newitem))



RE: Put each word in a quote in a new line in printout - Larz60+ - Feb-13-2018

Sorry, I'm not with it yet after my nap:
quote = "they stumble who run fast"
for x in quote.split():
    print(x)



RE: Put each word in a quote in a new line in printout - umarbilal - Apr-15-2019

Hello,

please see the solution below; Smile
# [ ] Print each word in the quote on a new line  
quote = "they stumble who run fast "
start = 0 
space_index = quote.find(" ")
while space_index != -1: 
    print(quote[start:space_index])
    start = space_index + 1
    space_index = quote.find(" ", space_index + 1)



RE: Put each word in a quote in a new line in printout - perfringo - Apr-15-2019

I think that most concise is to print split-uncpack with newline separator:

>>> quote = "they stumble who run fast"
>>> print(*quote.split(), sep='\n')
they
stumble
who
run
fast