Remove all newlines from inside a string

后端 未结 7 1883
自闭症患者
自闭症患者 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:38

    If the file includes a line break in the middle of the text neither strip() nor rstrip() will not solve the problem,

    strip family are used to trim from the began and the end of the string

    replace() is the way to solve your problem

    >>> my_name = "Landon\nWO"
    >>> print my_name
       Landon
       WO
    
    >>> my_name = my_name.replace('\n','')
    >>> print my_name
    LandonWO
    

提交回复
热议问题