如何在没有转义序列的情况下写新行
Hwo to write newline without escape sequence
我想知道如何写 \n
,而不是使用 \n
。 'raw' 写新行的方法是什么?
而不是
print("Hello\nWorld")
输出
Hello
World
我要
print("HelloSOMEENCODINGWorld)
输出
Hello
World
有没有办法在字符串中使用 ASCII、十六进制...?
其中一个选项是print('Hello{}World'.format(chr(10)))
。
一种方法可能是通过 os.linesep
import os
print('This is a line with a line break\nin the middle')
print(f'This is a line with a line break {os.linesep}in the middle')
但如前所述here:
Note: when writing to files using the Python API, do not use the os.linesep. Just use \n; Python automatically translates that to the proper newline character for your platform.
您可以使用多行字符串。
print("""Hello
World""")
不过\n更好
您可以使用 bash ANSI 转义序列:
print('Line1 3[1B3[50000DLine2',)
# 3[1B Gets cursor to the line below
# 3[50000D Gets the cursor 50000 spaces to the left , 50000 is just a random big number
您可以参考 this 了解有关 Bash ANSI 转义序列
的更多信息
我想知道如何写 \n
,而不是使用 \n
。 'raw' 写新行的方法是什么?
而不是
print("Hello\nWorld")
输出
Hello
World
我要
print("HelloSOMEENCODINGWorld)
输出
Hello
World
有没有办法在字符串中使用 ASCII、十六进制...?
其中一个选项是print('Hello{}World'.format(chr(10)))
。
一种方法可能是通过 os.linesep
import os
print('This is a line with a line break\nin the middle')
print(f'This is a line with a line break {os.linesep}in the middle')
但如前所述here:
Note: when writing to files using the Python API, do not use the os.linesep. Just use \n; Python automatically translates that to the proper newline character for your platform.
您可以使用多行字符串。
print("""Hello
World""")
不过\n更好
您可以使用 bash ANSI 转义序列:
print('Line1 3[1B3[50000DLine2',)
# 3[1B Gets cursor to the line below
# 3[50000D Gets the cursor 50000 spaces to the left , 50000 is just a random big number
您可以参考 this 了解有关 Bash ANSI 转义序列
的更多信息