I'm sort of mystified by the print hex to char conversion
Wojtek Walczak
gminick at bzt.bzt
Sat Apr 18 15:38:36 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...
...
>>>> print "\x63h\x61d"
> chad
>>>>
>
> Does print just do this magically?
Not only print does that:
>>> a='\x63h\x61d'
>>> a
'chad'
>>> print a
chad
>>> import sys
>>> sys.stdout.write(a)
chad>>> eval(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'chad' is not defined
The policy is described here:
http://www.python.org/doc/2.4.4/ref/strings.html
Unlike Standard C, all unrecognized escape sequences are left in the
string unchanged, i.e., the backslash is left in the string. (This
behavior is useful when debugging: if an escape sequence is mistyped,
the resulting output is more easily recognized as broken.)
So, as long as the escape sequence is recognized it is changed
accordingly.
--
Regards,
Wojtek Walczak,
http://tosh.pl/gminick/
More information about the Python-list
mailing list