开发者

How to print a string literally in Python

this is probably really simple but I can't find it.

I need to print what a string in Python contains. I'm collecting data from a serial port and I need to know if it is sending CR or CRLF + other control codes that are not ascii.

As an example say I had

开发者_运维知识库
s = "ttaassdd\n\rssleeroo"

then I would like to do is:

print s

Where it would show the \n\r rather than covert them into escape characters.


Try with:

print repr(s)
>>> 'ttaassdd\n\rssleeroo'


Saving your string as 'raw' string could also do the job.

(As in, by putting an 'r' in front of the string, like the example here)


    >>> s = r"ttaassdd\n\rssleeroo"
    >>> print s
    ttaassdd\n\rssleeroo

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜