the print statement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mirandacascade@yahoo.com

    #1

    the print statement

    O/S: Win2K
    Vsn of Python: 2.4

    Here is copy/paste from interactive window of pythonwin:
    [color=blue][color=green][color=darkred]
    >>> x = "Joe's desk"
    >>> y = 'Joe\x92s desk'
    >>> type(x)[/color][/color][/color]
    <type 'str'>[color=blue][color=green][color=darkred]
    >>> type(y)[/color][/color][/color]
    <type 'str'>[color=blue][color=green][color=darkred]
    >>> print x[/color][/color][/color]
    Joe's desk[color=blue][color=green][color=darkred]
    >>> print y[/color][/color][/color]
    Joe's desk[color=blue][color=green][color=darkred]
    >>> if x == y:[/color][/color][/color]
    .... print 'equal'
    .... else:
    .... print 'not equal'
    ....
    not equal[color=blue][color=green][color=darkred]
    >>> len(x)[/color][/color][/color]
    10[color=blue][color=green][color=darkred]
    >>> len(y)[/color][/color][/color]
    10[color=blue][color=green][color=darkred]
    >>> ord(x[3])[/color][/color][/color]
    39[color=blue][color=green][color=darkred]
    >>> ord(y[3])[/color][/color][/color]
    146[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    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?

  • Ben Finney

    #2
    Re: the print statement

    mirandacascade@ yahoo.com writes:
    [color=blue]
    > My questions are:[/color]

    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

    Comment

    • Tim Roberts

      #3
      Re: the print statement

      mirandacascade@ yahoo.com wrote:[color=blue]
      >
      >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?[/color]

      Sort of; it is the \x pair that signals this. This is in the Python
      documentation; the convention was borrowed from C.
      [color=blue]
      >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?[/color]

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

      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 ".
      [color=blue]
      >4) what is the role/function of the backslash character in the variable
      >y?[/color]

      See above. \x introduces a hex character. \047 is another special
      sequence; this is the octal code for apostrophe.
      [color=blue]
      >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
      >?[/color]

      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.
      [color=blue]
      >6) Would it be correct to infer that the print statement is aware of
      >characters beyond the 128 characters in the ascii character set?[/color]

      Certainly. It knows about whatever the current character set is.
      --
      - Tim Roberts, timr@probo.com
      Providenza & Boekelheide, Inc.

      Comment

      • mirandacascade@yahoo.com

        #4
        Re: the print statement

        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?

        Comment

        • Terry Reedy

          #5
          Re: the print statement


          <mirandacascade @yahoo.com> wrote in message
          news:1146960978 .564533.59730@j 73g2000cwa.goog legroups.com...[color=blue]
          > 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?[/color]

          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



          Comment

          • Kent Johnson

            #6
            Re: the print statement

            mirandacascade@ yahoo.com wrote:[color=blue]
            > 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?[/color]

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

            Kent

            Comment

            • Tim Roberts

              #7
              Re: the print statement

              Dennis Lee Bieber <wlfraed@ix.net com.com> wrote:
              [color=blue]
              >On Sun, 07 May 2006 00:09:06 GMT, Tim Roberts <timr@probo.com > declaimed
              >the following in comp.lang.pytho n:
              >[color=green]
              >> mirandacascade@ yahoo.com wrote:
              >>[color=darkred]
              >> >6) Would it be correct to infer that the print statement is aware of
              >> >characters beyond the 128 characters in the ascii character set?[/color]
              >>
              >> Certainly. It knows about whatever the current character set is.[/color]
              >
              > 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.[/color]

              Yes, I agree. I was too glib in my response.
              --
              - Tim Roberts, timr@probo.com
              Providenza & Boekelheide, Inc.

              Comment

              Working...