How to replace space in a string with \n

Michael Poeltl michael.poeltl at univie.ac.at
Thu Jan 31 04:37:52 EST 2019


hi,

^Bart <gabriele1NOSPAM at hotmail.com> ended in a Mail-Delivery...
so I send it ONLY to the python-list

^Bert, a proper way to do what you'd liked to achieve is the following:

>>> text = "The best day of my life!"
>>> newtext = '\n'.join( text.split() )
>>> print(newtext)
The
best
day
of
my
life!
>>>

regards
Michael

* ^Bart <gabriele1NOSPAM at hotmail.com> [2019-01-31 10:22]:
> 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!"
> 
> space = (' ')
> 
> if text.count(' ') in text:
>     space=\n
> 
> rightText = text-space
> 
>     print(rightText)
> 
> I should have an output like this:
> The
> best
> day
> of
> my
> life!
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list

-- 
  Michael Poeltl <michael.poeltl at univie.ac.at>
  Computational Materials Physics at University
  Wien, Sensengasse 8/12, A-1090 Wien, AUSTRIA
  http://cmp.univie.ac.at/
  http://homepage.univie.ac.at/michael.poeltl/
  using elinks-0.12, mutt-1.5.21, and vim-7.4,
  with python-3.6.1, on linux mint 17.3 (rose)   :-)
  fon: +43-1-4277-51409

  "Lehrend lernen wir!"



More information about the Python-list mailing list