Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python equivalent to echo -e?

Tags:

python

Is there a python equivalent to echo -e?

In other words, is there a built-in function to convert r"\x50\x79\x74\x68\x6f\x6e" to "Python" in Python?

Edit I added the 'r' prefix, to make sure everyone understands that I do not want the python interpreter to convert this. Rather, I want to convert that 24-character string to a 6-character one.

like image 510
Jonathon Reinhart Avatar asked Jan 22 '26 01:01

Jonathon Reinhart


1 Answers

The correct way to do this, which I just found is

>>> a = r"\x50\x79\x74\x68\x6f\x6e"
>>> print a
\x50\x79\x74\x68\x6f\x6e
>>> a.decode('string_escape')
'Python'

Make sure you are escaping the backslashes (or using the raw 'r' prefix) when testing this!

References:

  • http://docs.python.org/library/stdtypes.html#str.decode
  • http://docs.python.org/library/codecs.html#standard-encodings
like image 173
Jonathon Reinhart Avatar answered Jan 27 '26 02:01

Jonathon Reinhart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!