The new line feeds can be of the form \n
or \r
or both. We can remove them from a string using the strip()
method.
'this is a string\n'.strip()
And we get:
'this is a string'
Note that there is also the lstrip()
and rstrip()
for left and right stripping of the whitespaces in text. We can also be more specific by typing:
'this is a string\n'.strip("\n")
Or you can use the replace function. Assume that the input_text
has new line feeds in the middle of the string. We can clean them as follows:
input_txt = input_txt.replace('\n',' ')