且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Python中,打印字符串时是否可以转义换行符?

更新时间:2023-11-13 22:11:04

另一种可以使用转义字符停止python的方法是使用这样的原始字符串:

Another way that you can stop python using escape characters is to use a raw string like this:

>>> print(r"abc\ndef")
abc\ndef



or

>>> string = "abc\ndef"
>>> print (repr(string))
>>> 'abc\ndef'

只有使用 repr() / code>是将您的字符串放在单引号中,如果要使用引号

the only proplem with using repr() is that it puts your string in single quotes, it can be handy if you want to use a quote