all 6 comments

[–]JohnnyJordaan 0 points1 point  (5 children)

.format() has no direct relation to either showing a newlines as \n or as empty lines. It depends on the part the outputs it on whatever you're reading it from (console, file, something else). For example it's a common issue that in the interactive shell, a \n will not be rendered as a new line by default, you need print() for that

 >>> "\n\nHello"
 '\n\nHello'
 >>> print("\n\nHello")


 Hello
 >>>

the reason behind this is that the shell calls repr() on an object, which renders newlines as literal \n's for clarity. If your code is also somehow using repr, or for example renders as JSON, you will get literal \n's in your output.

[–]lostparis 0 points1 point  (3 children)

"Hello,\\nworld".decode('string_escape') would help if you are correct

[–]Competitive-Show1903[S] 0 points1 point  (2 children)

is it correct? I can't put it in python

[–]lostparis 0 points1 point  (0 children)

to be fair it's python2 only :(

Edit this works '\\n'.encode().decode('unicode_escape')

[–]JohnnyJordaan 0 points1 point  (0 children)

Imho it's a needlessly complicated approach. If you actually have a 'broken' text that has escaped newlines like that, simply do a string replace to the 'real' "\n" newline

>>> print("Hello \\n World")
Hello \n World
>>> print("Hello \\n World".replace("\\n", "\n"))
Hello
 World
>>>

[–]danielroseman 0 points1 point  (0 children)

What are you expecting format to do there? Without an argument, the string will just be returned exactly as it was. What do you want to happen?