Escaping the semicolon?

Discussion in 'Python' started by Nick, Dec 4, 2007.

  1. Nick

    Nick Guest

    Hi all,

    Is this expected behavior?

    >>> s = '123;abc'
    >>> s.replace(';', '\;')

    '123\\;abc'

    I just wanted a single backslash. I can see why this probably happens
    but i wondered if it is definitely intentional.

    Thanks
    Nick
     
    Nick, Dec 4, 2007
    #1
    1. Advertising

  2. Nick a écrit :
    > Hi all,
    >
    > Is this expected behavior?
    >
    >>>> s = '123;abc'
    >>>> s.replace(';', '\;')

    > '123\\;abc'


    >>> print s.replace(';', '\;')

    123\;abc

    > I just wanted a single backslash.


    You got it - even if it's not obvious !-)

    > I can see why this probably happens
    > but i wondered if it is definitely intentional.


    >>> s2 = '123\;abc'
    >>> s2

    '123\\;abc'
    >>> print s2

    123\;abc
    >>> list(s2)

    ['1', '2', '3', '\\', ';', 'a', 'b', 'c']

    As you can see, '\\' is counted as a single character !-)
    Since the backslash is the escape character, you need to escape it to
    have a litteral backslash:

    >>> s3 = '\'

    File "<stdin>", line 1
    s3 = '\'
    ^
    SyntaxError: EOL while scanning single-quoted string
    >>>
     
    Bruno Desthuilliers, Dec 4, 2007
    #2
    1. Advertising

  3. Nick wrote:

    > Hi all,
    >
    > Is this expected behavior?
    >
    >>>> s = '123;abc'
    >>>> s.replace(';', '\;')

    > '123\\;abc'
    >
    > I just wanted a single backslash. I can see why this probably happens
    > but i wondered if it is definitely intentional.


    There is only a single backslash. But the interactive prompt will use the
    repr()-function to print out returned values. Which will for strings print
    their escaped syntax.

    Try the above with a

    print s.replace(...)

    and you will see your desired outcome.

    diez
     
    Diez B. Roggisch, Dec 4, 2007
    #3
  4. Nick

    Tim Chase Guest

    > Is this expected behavior?
    >
    >>>> s = '123;abc'
    >>>> s.replace(';', '\;')

    > '123\\;abc'


    You're asking the interpreter to print a representation of your
    string, so it does so. Representations wrap the results in
    quotes and escape characters within that need escaping.

    >>> s.replace(';', '\;')

    '123\\;abc'
    >>> print repr(s.replace(';', '\;'))

    '123\\;abc'
    >>> print s.replace(';', '\;')

    123\;abc

    Additionally, it's best-practice to use raw strings or literal
    backslashes, making your replacement either

    r'\;'

    or

    '\\;'

    because depending on the character following the back-slash, it
    may be translated as a character you don't intend it to be.

    -tkc
     
    Tim Chase, Dec 4, 2007
    #4
  5. Nick

    Mel Guest

    Nick wrote:
    > Is this expected behavior?
    >
    >>>> s = '123;abc'
    >>>> s.replace(';', '\;')

    > '123\\;abc'
    >
    > I just wanted a single backslash. I can see why this probably happens
    > but i wondered if it is definitely intentional.


    What you're seeing on the screen is a "literalization" of the string
    value for the sake of the display. Consider

    Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
    [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> s = '123;abc'
    >>> b = s.replace(';', '\;')
    >>> b

    '123\\;abc'
    >>> len(b)

    8


    The length suggests that there's only one backslash in the string.


    Mel.

    On the other hand

    >>> repr(b)

    "'123\\\\;abc'"

    Isn't what I expected. No, wait, it is. It's the value of repr(b)
    repred by the Python display logic.

    MPW
     
    Mel, Dec 4, 2007
    #5
  6. Nick

    Nick Guest

    Thanks guys, you answered that interactive prompt question really
    clearly however, whats going on here. This works now -

    >>> working_string = '123;abc'
    >>> search_string = ';'
    >>> print working_string.replace(search_string, '\\' + search_string)

    123\;abc

    But this doesn't -

    ---
    import sys
    import string

    input = string.join(sys.argv[1:], '')
    escape_char_list = [ '(', '[]', ';', '^', '\\', '/', '|', '*', '$',
    '&', '[', ']', ')', '?' ]

    for ch in escape_char_list:
    input = input.replace(ch, '\\' + ch)

    print input
    ---

    Try "123 *?/ abc d;o /$'" as the argument... and you get -

    123 \*\?\/ abc d\\;o \/\$

    Still two back slashes, did i miss something very obvious? Sorry,
    sometimes these things are exasperating and just need more eyes or a
    head screwed on (if thats the case!).

    Nick
     
    Nick, Dec 4, 2007
    #6
  7. Nick

    Jerry Hill Guest

    On Dec 4, 2007 11:33 AM, Nick <> wrote:
    > Try "123 *?/ abc d;o /$'" as the argument... and you get -
    >
    > 123 \*\?\/ abc d\\;o \/\$


    That's because of the order you're doing the replacement. Put a print
    statement inside your for loop and you'll see something like this:

    input starts as "123 *?/ abc d;o /$'"
    Then when you replace ';' with '\;' you get input = "123 *?/ abc d\;o /$'"
    Then the next replacement replaces '\' with '\\' so you get input =
    "123 *?/ abc d\\;o /$'"

    If you move '\\' to the front of your list of replacement characters,
    things will probably work as you expect.

    --
    Jerry
     
    Jerry Hill, Dec 4, 2007
    #7
  8. Nick

    Nick Guest


    >
    > If you move '\\' to the front of your list of replacement characters,
    > things will probably work as you expect.
    >
    > --
    > Jerry


    I knew it would be something like that! Thanks for your help.
     
    Nick, Dec 4, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan
    Replies:
    3
    Views:
    4,603
  2. Russ

    orgin of semicolon usage

    Russ, Oct 24, 2003, in forum: C Programming
    Replies:
    9
    Views:
    505
    Derk Gwen
    Oct 26, 2003
  3. ankursinha
    Replies:
    33
    Views:
    22,344
    Keith Thompson
    Feb 25, 2004
  4. Prashanth Badabagni

    Printing "hello , wolrd" with out using semicolon

    Prashanth Badabagni, May 3, 2004, in forum: C Programming
    Replies:
    42
    Views:
    2,120
    Peter Nilsson
    May 12, 2004
  5. Peter Michaux

    Cookies: semicolon vs. semicolon-space

    Peter Michaux, Dec 10, 2006, in forum: Javascript
    Replies:
    3
    Views:
    221
    Michael Winter
    Dec 11, 2006
Loading...

Share This Page