How to Remove a Trailing Newline in Python
To remove a trailing newline (\n
) on a string in Python, use the rstrip()
function from the str
package like this:
string = 'abc\n'
new_str = str.rstrip(string)
print(new_str)
abc
str.rstrip()
does not modify the original string so the result will need to be stored in a new variable.