#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    6
    Rep Power
    0

    Backslash in strings


    Normally backslash is the essential part of escaping (\n, \b ...)

    But what to do to insert "\" in a string?
    Code:
    x= 'sd\&gh'  ---> 'sd\\&gh'
    Also 'raw' string is useless:
    Code:
    x=r"sd\&gh"  --->  'sd\\&gh'
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    You can use '\\'
    grim
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Originally Posted by fiomba
    Normally backslash is the essential part of escaping (\n, \b ...)

    But what to do to insert "\" in a string?
    Code:
    x= 'sd\&gh'  ---> 'sd\\&gh'
    That's it, it has worked.
    When you put "\n" it is the escape sequence for a newline, but when you put "\&", that is not an escape sequence, so it is treated as a backslash and an ampersand as you want.

    The reason it becomes "\\" when you print it is because that's the escape sequence to put a backslash in, as Grim Archon commented.

    Also 'raw' string is useless:
    Code:
    x=r"sd\&gh"  --->  'sd\\&gh'
    That is also working.

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    35
    Rep Power
    10
    """ \ """

IMN logo majestic logo threadwatch logo seochat tools logo