the print statement

M

mirandacascade

O/S: Win2K
Vsn of Python: 2.4

Here is copy/paste from interactive window of pythonwin:
.... print 'equal'
.... else:
.... print 'not equal'
....
not equal
len(x) 10
len(y) 10
ord(x[3]) 39
ord(y[3]) 146

My questions are:
1) is the 'x' character within the variable y a signal that what
follows is a hex value?
2) is it more than just a coincidence that 146 (the result of
ord(y[3])) is the decimal equivalent of the hex number 92?
3) is there any character set in which 146 represents the
single-quote/apostrophe character? if so, which character set?
4) what is the role/function of the backslash character in the variable
y?
5) how did the print statement know to transform the contents of y
('Joe\x92s desk') to something that gets displayed as:

Joe's desk

?

6) Would it be correct to infer that the print statement is aware of
characters beyond the 128 characters in the ascii character set?
 
T

Tim Roberts

O/S: Win2K
Vsn of Python: 2.4

Here is copy/paste from interactive window of pythonwin:
...
My questions are:
1) is the 'x' character within the variable y a signal that what
follows is a hex value?

Sort of; it is the \x pair that signals this. This is in the Python
documentation; the convention was borrowed from C.
2) is it more than just a coincidence that 146 (the result of
ord(y[3])) is the decimal equivalent of the hex number 92?

Did you really have to ask that question? The 4th character of Y is a byte
containing 0x92, which is 146 in decimal.
3) is there any character set in which 146 represents the
single-quote/apostrophe character? if so, which character set?

Certainly. The default Windows code page, CP 1252, is an extension to
ISO-8859-1 which includes this. It defines 0x91 as a "left single quote"
and 0x92 as a "right single quote". In typography, you often want to use
different quotes for something like 'this' than you do for something like
isn't. 0x91 and 0x92 are used for 'this', and 0x27 is used for isn't.

Your VGA font happens to display "left single quote" and "right single
quote" with the same glyph as "apostrophe".
4) what is the role/function of the backslash character in the variable
y?

See above. \x introduces a hex character. \047 is another special
sequence; this is the octal code for apostrophe.
5) how did the print statement know to transform the contents of y
('Joe\x92s desk') to something that gets displayed as:

Joe's desk
?

The print statement didn't know that. It sent the 0x92 character. It's
just that your VGA font happens to display them as the same glyph.
6) Would it be correct to infer that the print statement is aware of
characters beyond the 128 characters in the ascii character set?

Certainly. It knows about whatever the current character set is.
 
M

mirandacascade

Thank you. Yes, that post answers most of the questions. I now have a
bit of an understanding of the \xhh pattern. It's still unclear to me,
however, how one can go from the \x92 pattern and arrive at the
apostrophe character. Is \x92 theh apostrophe character in another
character set? If so, which character set?
 
T

Terry Reedy

bit of an understanding of the \xhh pattern. It's still unclear to me,
however, how one can go from the \x92 pattern and arrive at the
apostrophe character. Is \x92 the apostrophe character in another
character set? If so, which character set?

What you see with "print '\x92'" and indeed any value above '\x7F' is
situation-dependent. On my WinXP system, typing that in the Python command
window gives the AE ligature (ie, the two letters joined together). Doing
the same in the IDLE shell window gives an accent mark similar to ` but
slanting the other way. An apostrophe ' in the same window is vertical, so
they are different characters.

Terry Jan Reedy
 
D

Dennis Lee Bieber

Certainly. It knows about whatever the current character set is.

I'd argue that the print statement actually doesn't care... All it
does is send the bytes making up a string to whatever display driver is
active, and it is the driver that converts those bytes into a visible
display.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
T

Tim Roberts

Dennis Lee Bieber said:
I'd argue that the print statement actually doesn't care... All it
does is send the bytes making up a string to whatever display driver is
active, and it is the driver that converts those bytes into a visible
display.

Yes, I agree. I was too glib in my response.
 

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
474,294
Messages
2,571,511
Members
48,213
Latest member
DonnellTol

Latest Threads

Top