pythonansi-escape

How to print a string literally in Python


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.


Solution

  • Python 2

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

    Python 3

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