How to replace space in a string with \n

Antoon Pardon antoon.pardon at vub.be
Thu Jan 31 05:14:11 EST 2019


On 31/01/19 10:18, ^Bart 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:

Have you even tried to run this?

>
> text = "The best day of my life!"
>
> space = (' ')
>
> if text.count(' ') in text:
>     space=\n

I don't think this does what you think it does.

text.count(' ') will return 5, an integer. So you are testing if 5 is in text. But since
5 is an integer that will raise a TypeError.

>
> rightText = text-space


Where does text-space come from?

-- 
Antoon.




More information about the Python-list mailing list