Remove all line breaks from a long string of text

前端 未结 8 1939
长发绾君心
长发绾君心 2020-11-28 19:53

Basically, I\'m asking the user to input a string of text into the console, but the string is very long and includes many line breaks. How would I take the user\'s string a

8条回答
  •  时光说笑
    2020-11-28 20:40

    You can split the string with no separator arg, which will treat consecutive whitespace as a single separator (including newlines and tabs). Then join using a space:

    In : " ".join("\n\nsome    text \r\n with multiple whitespace".split())
    Out: 'some text with multiple whitespace'
    

    https://docs.python.org/2/library/stdtypes.html#str.split

提交回复
热议问题