How can I remove a trailing newline?

前端 未结 28 3538
感动是毒
感动是毒 2020-11-21 23:27

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

28条回答
  •  执念已碎
    2020-11-21 23:55

    "line 1\nline 2\r\n...".replace('\n', '').replace('\r', '')
    >>> 'line 1line 2...'
    

    or you could always get geekier with regexps :)

    have fun!

提交回复
热议问题