471,119 Members | 1,429 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,119 software developers and data experts.

the print statement

O/S: Win2K
Vsn of Python: 2.4

Here is copy/paste from interactive window of pythonwin:
x = "Joe's desk"
y = 'Joe\x92s desk'
type(x) <type 'str'> type(y) <type 'str'> print x Joe's desk print y Joe's desk if x == y: .... 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?

May 6 '06 #1
6 2400
mi************@yahoo.com writes:
My questions are:


Mostly answered in the language reference:

<URL:http://docs.python.org/ref/strings.html>

--
\ "Facts are meaningless. You could use facts to prove anything |
`\ that's even remotely true!" -- Homer, _The Simpsons_ |
_o__) |
Ben Finney

May 6 '06 #2
mi************@yahoo.com wrote:

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.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
May 7 '06 #3
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?

May 7 '06 #4

<mi************@yahoo.com> wrote in message
news:11*********************@j73g2000cwa.googlegro ups.com...
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

May 7 '06 #5
mi************@yahoo.com wrote:
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?


\x92 is a right single quote in Windows cp1252.
http://www.microsoft.com/globaldev/r...sbcs/1252.mspx

Kent
May 8 '06 #6
Dennis Lee Bieber <wl*****@ix.netcom.com> wrote:
On Sun, 07 May 2006 00:09:06 GMT, Tim Roberts <ti**@probo.com> declaimed
the following in comp.lang.python:
mi************@yahoo.com wrote:
>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.


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.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
May 10 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by mmccaws | last post: by
30 posts views Thread by Martin Bless | last post: by
12 posts views Thread by Michael Foord | last post: by
14 posts views Thread by Marcin Ciura | last post: by
2 posts views Thread by jamesthiele.usenet | last post: by
69 posts views Thread by Edward K Ream | last post: by
2 posts views Thread by Phoe6 | last post: by
5 posts views Thread by xirowei | last post: by
7 posts views Thread by samslists | last post: by
10 posts views Thread by Prisoner at War | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.