Escaping the semicolon?

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

  1. Nick

    Nick Guest

    Hi all,

    Is this expected behavior?

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

    Nick, Dec 4, 2007
    1. Advertisements

  2. Nick a écrit :
    You got it - even if it's not obvious !-)
    ['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:
    File "<stdin>", line 1
    s3 = '\'
    SyntaxError: EOL while scanning single-quoted string
    Bruno Desthuilliers, Dec 4, 2007
    1. Advertisements

  3. 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 B. Roggisch, Dec 4, 2007
  4. Nick

    Tim Chase Guest

    Is this expected behavior?
    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.

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




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

    Tim Chase, Dec 4, 2007
  5. Nick

    Mel Guest

    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.8

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


    On the other hand

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

    Mel, Dec 4, 2007
  6. Nick

    Nick Guest

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

    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, Dec 4, 2007
  7. Nick

    Jerry Hill Guest

    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 Hill, Dec 4, 2007
  8. Nick

    Nick Guest

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.