String backslash characters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PD

    #1

    String backslash characters

    Hello,

    I am new to python, but i am quite curious about the following.

    suppose you had

    print '\378'

    which should not work because \377 is the max. then it displays two
    characters (an 8 and a heart in my case...). What else does'nt quite
    make sense is that if this is an octal why is an 8 accepted?

    for instance is 378 really 11, 111, 1000 which is then the two
    characters: <00000001>,<111 1000>. And why is this accepted?

    I apologize if this has been discussed or if it is obvious. I would
    appreciate it if someone could clear me up.

    Yours,
    PD

  • Dan Bishop

    #2
    Re: String backslash characters

    PD wrote:[color=blue]
    > Hello,
    >
    > I am new to python, but i am quite curious about the following.
    >
    > suppose you had
    >
    > print '\378'
    >
    > which should not work because \377 is the max. then it displays two
    > characters (an 8 and a heart in my case...). What else does'nt quite
    > make sense is that if this is an octal why is an 8 accepted?[/color]

    Because 8 isn't an octal digit, so it's not part of the escape
    sequence, but a separate character. It's just like

    print 'This string\47s escape sequence does not include the s.'

    Your example displays two characters because it is two characters:
    '\037' and '8'.

    Comment

    • Leif K-Brooks

      #3
      Re: String backslash characters

      PD wrote:[color=blue]
      > Hello,
      >
      > I am new to python, but i am quite curious about the following.
      >
      > suppose you had
      >
      > print '\378'
      >
      > which should not work because \377 is the max. then it displays two
      > characters (an 8 and a heart in my case...). What else does'nt quite
      > make sense is that if this is an octal why is an 8 accepted?[/color]

      It's not doing quite what you think. The '\37' is seen as octal, but
      since '8' isn't a valid octal digit, it's seen as the literal character '8'.

      In other words, these two lines are equivalent:[color=blue][color=green][color=darkred]
      >>> '\378'[/color][/color][/color]
      '\x1f8'[color=blue][color=green][color=darkred]
      >>> '\37' + '8'[/color][/color][/color]
      '\x1f8'

      Comment

      • Binu K S

        #4
        Re: String backslash characters

        '\378' becomes a two character string. The first character is '\37'
        and the second character is '8'.
        [color=blue][color=green][color=darkred]
        >>> str = '\378'
        >>> str[0][/color][/color][/color]
        '\x1f'[color=blue][color=green][color=darkred]
        >>> str[1][/color][/color][/color]
        '8'[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        On 23 Dec 2004 20:53:13 -0800, PD <albaalu@gmail. com> wrote:[color=blue]
        > Hello,
        >
        > I am new to python, but i am quite curious about the following.
        >
        > suppose you had
        >
        > print '\378'
        >
        > which should not work because \377 is the max. then it displays two
        > characters (an 8 and a heart in my case...). What else does'nt quite
        > make sense is that if this is an octal why is an 8 accepted?
        >
        > for instance is 378 really 11, 111, 1000 which is then the two
        > characters: <00000001>,<111 1000>. And why is this accepted?
        >
        > I apologize if this has been discussed or if it is obvious. I would
        > appreciate it if someone could clear me up.
        >
        > Yours,
        > PD
        >
        > --
        > http://mail.python.org/mailman/listinfo/python-list
        >[/color]

        Comment

        • Benji York

          #5
          Re: String backslash characters

          PD wrote:[color=blue]
          > I am new to python, but i am quite curious about the following.
          >
          > print '\378'
          >
          > which should not work because \377 is the max. then it displays two
          > characters (an 8 and a heart in my case...). What else does'nt quite
          > make sense is that if this is an octal why is an 8 accepted?[/color]

          It would appear that the parser reads \377 as a single character and
          \378 as two (\37 and the "8" character). I'm somewhat surprised you're
          seeing a heart and an 8. What OS/language combination are you using?
          If you're using English Windows you can get a heart and an "8" with
          "print '\38'".
          --
          Benji York
          benji@benjiyork .com

          Comment

          Working...