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

G

grocery_stocker

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@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.
Does print just do this magically?
 
P

Peter Pearson

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@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.
Does print just do this magically?

I'm not sure what magic you see in there, but does the
following help?
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.
 
D

Dave Angel

grocery_stocker said:
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@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.
chad
chad
chad
chad


Does print just do this magically?
print() isn't doing anything special with these strings. It's just
sending the characters to stdout. The question is what are the characters.

Whenever you're defining a quote literal in your code, there are rules
about how characters are interpreted on their way to the string. And
these rules are different for ascii strings, for unicode strings, and
for raw strings (all prefixes to the leading quote sign. I'll just talk
about the ascii strings.

In order to let you enter characters into a string that would otherwise
be difficult (like newline, which has a special meaning, or backspace,
which is tricky to type in most text editors), the backslash is defined
as an escape character. Whatever follows the backslash is interpreted
specially. One case is the \n, which represents a newline. Another is
\t, which represents tab. Another is \xdd which is used to represent
an arbitrary code, given its hex representation. There are others.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top