Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Python by (47.6k points)

What is the Python equivalent of Perl's chomp function, which removes the last character of a string if it is a newline?

1 Answer

0 votes
by (106k points)

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']

Related questions

+1 vote
1 answer
+1 vote
2 answers
asked Jul 9, 2019 in Python by selena (1.6k points)
0 votes
3 answers
0 votes
2 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...