1

I am looking for a way to prefix strings in python with a single backslash, e.g. "]" -> "]". Since "\" is not a valid string in python, the simple

mystring = '\' + mystring

won't work. What I am currently doing is something like this:

mystring = r'\@@@' + mystring
mystring.replace('@@@','')

While this works most of the time, it is not elegant and also can cause problems for strings containing "@@@" or whatever the "filler" is set to. Is there a bette way of doing this?

2
  • Just use mystring = '\\' + mystring -- you must "escape" literal backslash characters in a string with another backslash. Commented Apr 4, 2014 at 16:04
  • See the line starting with "The backslash () character is used to escape characters..." in the String Literals section of the documentation. Commented Apr 4, 2014 at 16:10

4 Answers 4

3

You need to escape the backslash with a second one, to make it a literal backslash:

mystring = "\\" + mystring

Otherwise it thinks you're trying to escape the ", which in turn means you have no quote to terminate the string

Ordinarily, you can use raw string notation (r'string'), but that won't work when the backslash is the last character


The difference between print a and just a:

>>> a = 'hello'
>>> a = '\\' + a
>>> a
'\\hello'
>>> print a
\hello
Sign up to request clarification or add additional context in comments.

1 Comment

It does work. But when you just type a it prints the escaped string (the repr). print a displays it correctly
2

Python strings have a feature called escape characters. These allow you to do special things inside as string, such as showing a quote (" or ') without closing the string you're typing

See this table

So when you typed

mystring = '\' + mystring

the \' is an escaped apostrophe, meaning that your string now has an apostrophe in it, meaning it isn't actually closed, which you can see because the rest of that line is coloured.

To type a backslash, you must escape one, which is done like this:

>>> aBackSlash = '\\'
>>> print(aBackSlash)
\

1 Comment

To all other answerers: thank you for your support - from all the valid answers it was hard to choose, since most of them are rather similar. sorry for not choosing yours :)
1

You should escape the backslash as follows:

mystring = "\\" + mystring

This is because if you do '\' it will end up escaping the second quotation. Therefore to treat the backslash literally, you must escape it.

Examples

>>> s = 'hello'
>>> s = '\\' + s
>>> print
\hello

Your case

>>> mystring = 'it actually does work'
>>> mystring = '\\' + mystring
>>> print mystring
\it actually does work

1 Comment

@Isaac, Actually there is a difference between printing a string and display its values. See my answer
1

As a different way of approaching the problem, have you considered string formatting?

r'\%s' % mystring

or:

r'\{}'.format(mystring)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.