How to print a string without including '\n' in Python

前端 未结 8 994
没有蜡笔的小新
没有蜡笔的小新 2021-01-01 16:26

Suppose my string is:

\' Hai Hello\\nGood eve\\n\'

How do I eliminate the \'\\n\' in between and make a string print like :

相关标签:
8条回答
  • 2021-01-01 16:50

    I realize this is a terribly old post, but I just ran into this also and wanted to add to it for clarity. What the original user is asking, I believe, is how to get the OS to interpret the string "some text\nsome more text" as:

    some text

    some more text

    Instead of just printing:

    "some text\nsome more text"

    and the answer is it is already interpreting. In ubuntu I ran into the problem of it escaping the newline characters when putting it into the string without me asking it to. So the string:

    "some text\nsome more text"

    is in fact

    "some text\\nsome more text"

    All that is required is to use the mystring.replace("\\\n", "\n") and the desired output will be achieved. Hope this is clear and helps some future person.

    0 讨论(0)
  • 2021-01-01 16:57
    >>> 'Hai Hello\nGood eve\n'.replace('\n', ' ')
    'Hai Hello Good eve '
    
    0 讨论(0)
提交回复
热议问题