将未转义的白色空间打印到外壳

Tri*_*daz 7 python escaping

考虑这行Python代码:

s = "This string has \n\r whitespace"
Run Code Online (Sandbox Code Playgroud)

我该怎么做

print s

给我吗

This string has \n\r whitespace

代替

This string has
whitespace
Run Code Online (Sandbox Code Playgroud)

就像现在一样.

Mic*_*jer 18

你想要原始字符串吗?

s = r"This string has \n\r whitespace"
Run Code Online (Sandbox Code Playgroud)

或者将特殊字符转换为它的表示?

repr(s)
Run Code Online (Sandbox Code Playgroud)


Woo*_*ble 8

 print s.encode('string-escape')
Run Code Online (Sandbox Code Playgroud)