I'm sort of mystified by the print hex to char conversion

Peter Pearson ppearson at nowhere.invalid
Sat Apr 18 16:51:22 EDT 2009


On Sat, 18 Apr 2009 11:45:09 -0700 (PDT), grocery_stocker wrote:
> I'm just really not seeing how something like x63 and/or x61 gets
> converted by 'print' to the corresponding chars in the following
> output...
>
> [cdalten at localhost oakland]$ python
> Python 2.4.3 (#1, Oct  1 2006, 18:00:19)
> [GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> print '\x63had'
> chad
>>>> print '\x63h\x61d'
> chad
>>>> print "\x63had"
> chad
>>>> print "\x63h\x61d"
> chad
>>>>
>
> Does print just do this magically?

I'm not sure what magic you see in there, but does the
following help?

>>> a = "\x63had"
>>> len(a)
4
>>> b = "\x63h\x61d"
>>> len(b)
4

Personally, when I'm puzzled by something involving a string
with backslashes, I use len to help figure out what's really
in the string and what's inserted on the way to my screen.

-- 
To email me, substitute nowhere->spamcop, invalid->net.



More information about the Python-list mailing list