How can I remove a trailing newline?

前端 未结 28 3527
感动是毒
感动是毒 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:50

    If your question is to clean up all the line breaks in a multiple line str object (oldstr), you can split it into a list according to the delimiter '\n' and then join this list into a new str(newstr).

    newstr = "".join(oldstr.split('\n'))

提交回复
热议问题