I like the way Python interactive interpreter prints strings, and I want to repeat that specifically in scripts. However, I can't seem to do that.
Example. I can do this in interpreter:
>>> a="d\x04"
>>> a
'd\x04'
However, I cannot replicate this in the python itself
$ python -c 'a="d\x04";print a'
d
I want this because I want to debug a code with a lot of string with similar non-printable characters.
Is there an easy way to do this?
Oh, that was fast.
I can just use repr()
functon. That is, in my example,
python -c 'a="d\x04";print repr(a)'
You're looking for repr()
:
>>> a = 'd\x04'
>>> a
'd\x04'
>>> print(a)
d
>>> repr(a)
"'d\\x04'"
>>> print(repr(a))
'd\x04'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With