[Python] Converting, Replacing, and Removing Line Breaks with string.replace()

Tadashi Shigeoka ·  Tue, January 17, 2012

I’ll introduce methods to convert, replace, and remove line breaks in Python.

Python

You can use string.replace() to appropriately replace line break codes contained in strings.

If you want to remove line breaks, you can replace them with an empty string ” as follows.

>>> text = 'こんにちは。\\r\nありがとう!'
>>> print text
こんにちは。
ありがとう!
>>> text = text.replace('\\r\n','')
>>> print text
こんにちは。ありがとう!

Reference Information

That’s all from the Gemba.