How can I remove a trailing newline?

前端 未结 28 4097
感动是毒
感动是毒 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-22 00:02

    You may use line = line.rstrip('\n'). This will strip all newlines from the end of the string, not just one.

提交回复
热议问题