How to replace space in a string with \n

Grant Edwards grant.b.edwards at gmail.com
Thu Jan 31 13:49:50 EST 2019


On 2019-01-31, ^Bart <gabriele1NOSPAM at hotmail.com> wrote:
> Hello everybody! :)
>
> I got a text and I should replace every space with \n without to use 
> str.replace, I thought something like this:
>
> text = "The best day of my life!"

[...]

> I should have an output like this:
> The
> best
> day
> of
> my
> life!

Here's a solution. But don't turn it in. You're not supposed to know
how to do this yet, and your instructor will know you copied it from the
internet:

print(''.join('\n' if c == ' ' else c for c in text))

-- 
Grant Edwards               grant.b.edwards        Yow! over in west
                                  at               Philadelphia a puppy is
                              gmail.com            vomiting ...




More information about the Python-list mailing list