Back
What is the Python equivalent of Perl's chomp function, which removes the last character of a string if it is a newline?
To remove a trailing newline you can use splitlines():-
>>> text = "line 1\nline 2\r\nline 3\nline 4" >>> text.splitlines() ['line 1', 'line 2', 'line 3', 'line 4']
>>> text = "line 1\nline 2\r\nline 3\nline 4"
>>> text.splitlines()
['line 1', 'line 2', 'line 3', 'line 4']
31k questions
32.8k answers
501 comments
693 users