Print string "how" interactive interpreter?

I love the way the interactive Python interpreter prints lines, and I want to repeat this specifically in scripts. However, I cannot do this.

Example. I can do this in the interpreter:

>>> a="d\x04"
>>> a
'd\x04'

However I cannot replicate this in python itself

$ python -c 'a="d\x04";print a'
d

I want this because I want to debug code with a lot of lines with similar non-printable characters.

Is there an easy way to do this?

+4
source share
3 answers

You are looking for repr():

>>> a = 'd\x04'
>>> a
'd\x04'
>>> print(a)
d
>>> repr(a)
"'d\\x04'"
>>> print(repr(a))
'd\x04'
+4
source

Oh, that was fast.

I can use repr()functon. That is, in my example,

python -c 'a="d\x04";print repr(a)'
+6
source

( )

$ python -c 'a=r"d\x04";print a'
d\x04
$ python -c 'a="d\x04";print "%r"%a'
d\x04

, , repr(), .

0

Source: https://habr.com/ru/post/1568978/


All Articles