Remove all newlines from inside a string

后端 未结 7 1893
自闭症患者
自闭症患者 2020-11-28 09:02

I\'m trying to remove all newline characters from a string. I\'ve read up on how to do it, but it seems that I for some reason am unable to do so. Here is step by step what

7条回答
  •  我在风中等你
    2020-11-28 09:34

    strip() returns the string with leading and trailing whitespaces(by default) removed.

    So it would turn " Hello World " to "Hello World", but it won't remove the \n character as it is present in between the string.

    Try replace().

    str = "Hello \n World"
    str2 = str.replace('\n', '')
    print str2
    

提交回复
热议问题