what is wrong with that r"\"

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

    #1

    what is wrong with that r"\"

    question without words:
    >>r"\"
    File "<stdin>", line 1
    r"\"
    ^
    SyntaxError: EOL while scanning single-quoted string
    >>r"\ "
    '\\ '
  • kyosohma@gmail.com

    #2
    Re: what is wrong with that r&quot;\&quot ;

    On Jul 3, 7:15 am, alf <ask@mewrote:
    question without words:
    >
    >>r"\"
    File "<stdin>", line 1
    r"\"
    ^
    SyntaxError: EOL while scanning single-quoted string
    >>r"\ "
    '\\ '
    One slash escapes the following character, so the proper way of
    writing it is either

    r"\\" or r"\""

    See http://docs.python.org/ref/strings.html for more information.

    Mike

    Comment

    • kyosohma@gmail.com

      #3
      Re: what is wrong with that r&quot;\&quot ;

      On Jul 3, 8:25 am, Jean-Paul Calderone <exar...@divmod .comwrote:
      On Tue, 03 Jul 2007 06:16:43 -0700, kyoso...@gmail. com wrote:
      On Jul 3, 7:15 am, alf <ask@mewrote:
      question without words:
      >
      >>r"\"
      File "<stdin>", line 1
      r"\"
      ^
      SyntaxError: EOL while scanning single-quoted string
      >>r"\ "
      '\\ '
      >
      One slash escapes the following character, so the proper way of
      writing it is either
      >
      r"\\" or r"\""
      >
      Seehttp://docs.python.org/ref/strings.htmlfor more information.
      >
      I wonder if the OP was asking how to spell the one-length string \?
      In that case, the answer is that it can't be done using raw strings,
      but "\\" does it. Backslash escapes aren't interpreted in raw strings,
      but you still can't end a raw string with a backslash.
      >
      Jean-Paul
      Very true...sometime s I need to read these weird posts 2 or 3 times.

      Mike

      Comment

      • Neil Cerutti

        #4
        Re: what is wrong with that r&quot;\&quot ;

        On 2007-07-03, alf <ask@mewrote:
        question without words:
        >
        >r"\"
        File "<stdin>", line 1
        r"\"
        ^
        SyntaxError: EOL while scanning single-quoted string
        >r"\ "
        '\\ '
        From the Python Language Reference 2.4.1 String Literals:

        When an "r" or "R" prefix is present, a character following a
        backslash is included in the string without change, and all
        backslashes are left in the string. For example, the string
        literal r"\n" consists of two characters: a backslash and a
        lowercase "n". String quotes can be escaped with a backslash,
        but the backslash remains in the string; for example, r"\"" is
        a valid string literal consisting of two characters: a
        backslash and a double quote; r"\" is not a valid string
        literal (even a raw string cannot end in an odd number of
        backslashes). Specifically, a raw string cannot end in a
        single backslash (since the backslash would escape the
        following quote character). Note also that a single backslash
        followed by a newline is interpreted as those two characters
        as part of the string, not as a line continuation.

        --
        Neil Cerutti
        Ask about our plans for owning your home --sign at mortgage company

        Comment

        • Nick Craig-Wood

          #5
          Re: what is wrong with that r&quot;\&quot ;

          Neil Cerutti <horpner@yahoo. comwrote:
          From the Python Language Reference 2.4.1 String Literals:
          >
          When an "r" or "R" prefix is present, a character following a
          backslash is included in the string without change, and all
          backslashes are left in the string. For example, the string
          literal r"\n" consists of two characters: a backslash and a
          lowercase "n".
          So far so good.
          String quotes can be escaped with a backslash,
          but the backslash remains in the string; for example, r"\"" is
          a valid string literal consisting of two characters: a
          backslash and a double quote;
          a) That is weird! Why would you ever want to do that - ie insert \"
          into your string as a special case? If I wanted a " in a raw string
          then I'd use a r'' string or a triple quoted string.
          r"\" is not a valid string literal (even a raw string cannot end
          in an odd number of backslashes). Specifically, a raw string
          cannot end in a single backslash (since the backslash would
          escape the following quote character).
          b) That is a logical consequence of a)
          Note also that a single backslash followed by a newline is
          interpreted as those two characters as part of the string, not
          as a line continuation.
          As I'd expect.

          If we removed a) then we could remove b) also and r"" strings would
          work as everyone expects.

          Does anyone know the justification for a)? Maybe we should remove it
          in py3k?

          --
          Nick Craig-Wood <nick@craig-wood.com-- http://www.craig-wood.com/nick

          Comment

          • Nick Craig-Wood

            #6
            Re: what is wrong with that r&quot;\&quot ;

            kyosohma@gmail. com <kyosohma@gmail .comwrote:
            On Jul 3, 7:15 am, alf <ask@mewrote:
            question without words:
            >>r"\"
            File "<stdin>", line 1
            r"\"
            ^
            SyntaxError: EOL while scanning single-quoted string
            >>r"\ "
            '\\ '
            >
            One slash escapes the following character, so the proper way of
            writing it is either
            >
            r"\\" or r"\""
            I don't think so...
            >>r"\\"
            '\\\\'
            >>r"\""
            '\\"'

            Indicating that all the \ in the above are inserted literally.

            Maybe you meant
            >>"\\"
            '\\'
            >>"\""
            '"'

            --
            Nick Craig-Wood <nick@craig-wood.com-- http://www.craig-wood.com/nick

            Comment

            • Neil Cerutti

              #7
              Re: what is wrong with that r&quot;\&quot ;

              On 2007-07-04, Nick Craig-Wood <nick@craig-wood.comwrote:
              > String quotes can be escaped with a backslash, but the
              > backslash remains in the string; for example, r"\"" is a
              > valid string literal consisting of two characters: a
              > backslash and a double quote;
              >
              a) That is weird! Why would you ever want to do that - ie
              insert \" into your string as a special case? If I wanted a "
              in a raw string then I'd use a r'' string or a triple quoted
              string.
              >
              If we removed a) then we could remove b) also and r"" strings
              would work as everyone expects.
              >
              Does anyone know the justification for a)? Maybe we should
              remove it in py3k?
              If the escaped quotes didn't function in raw strings, I'd be
              unable to construct (with a single notation) a regex that
              included both kinds of quotes at once.

              re.compile(r"'\ "")

              --
              Neil Cerutti

              Comment

              • Marc 'BlackJack' Rintsch

                #8
                Re: what is wrong with that r&quot;\&quot ;

                On Wed, 04 Jul 2007 11:21:14 +0000, Neil Cerutti wrote:
                If the escaped quotes didn't function in raw strings, I'd be
                unable to construct (with a single notation) a regex that
                included both kinds of quotes at once.
                >
                re.compile(r"'\ "")
                Where's the problem!? ::

                re.compile(r''' '"''')

                Ah, I see -- readability is the problem. :-)

                Ciao,
                Marc 'BlackJack' Rintsch

                Comment

                • Duncan Booth

                  #9
                  Re: what is wrong with that r&quot;\&quot ;

                  Nick Craig-Wood <nick@craig-wood.comwrote:
                  Does anyone know the justification for a)? Maybe we should remove it
                  in py3k?
                  >
                  I think at least part of the justification is that it keeps the grammar
                  simple. The tokenising happens identically irrespective of any modifiers.
                  The r modifier doesn't actually change the syntax of the string at all,
                  just the interpretation of the token.

                  If you changed this then the grammar would become slightly more complex.

                  Comment

                  • Neil Cerutti

                    #10
                    Re: what is wrong with that r&quot;\&quot ;

                    On 2007-07-04, Marc 'BlackJack' Rintsch <bj_666@gmx.net wrote:
                    On Wed, 04 Jul 2007 11:21:14 +0000, Neil Cerutti wrote:
                    >
                    >If the escaped quotes didn't function in raw strings, I'd be
                    >unable to construct (with a single notation) a regex that
                    >included both kinds of quotes at once.
                    >>
                    > re.compile(r"'\ "")
                    >
                    Where's the problem!? ::
                    >
                    re.compile(r''' '"''')
                    That incurs the problem that you can't end with a '. So you can't
                    end with *something* either way.

                    --
                    Neil Cerutti

                    Comment

                    • Neil Cerutti

                      #11
                      Re: what is wrong with that r&quot;\&quot ;

                      On 2007-07-04, Neil Cerutti <horpner@yahoo. comwrote:
                      On 2007-07-04, Marc 'BlackJack' Rintsch <bj_666@gmx.net wrote:
                      >On Wed, 04 Jul 2007 11:21:14 +0000, Neil Cerutti wrote:
                      >>
                      >>If the escaped quotes didn't function in raw strings, I'd be
                      >>unable to construct (with a single notation) a regex that
                      >>included both kinds of quotes at once.
                      >>>
                      >> re.compile(r"'\ "")
                      >>
                      >Where's the problem!? ::
                      >>
                      > re.compile(r''' '"''')
                      >
                      That incurs the problem that you can't end with a '. So you
                      can't end with *something* either way.
                      I take that back. With good planning, you can end with whichever
                      you need by choosing the other delimiter.

                      Oh well. It seemed like a good explanation at first.

                      --
                      Neil Cerutti

                      Comment

                      Working...