2

When I run this in python:

print "\bin\example.exe"

it prints

enter image description here

I think this is because the "\b" is being picked up as a regular expression.

Any help is appreciated!

2
  • Use double backslash (i.e., ` \\ ` instead of ` \ `). Commented Apr 2, 2015 at 4:21
  • try print r"\bin\example.exe" Commented Apr 2, 2015 at 4:21

4 Answers 4

3

You are correct about the \b being the issue, it is using \ as the escape character.

There are two ways of dealing with backslashes in Python:

  • The first is to use a double back slash "\\" which prints as \; and
  • The second is to put a r before the string to everything is printed as written such as r"asdf\jk" which will print asdf\jk.
Sign up to request clarification or add additional context in comments.

Comments

2

You would generally escape out \ with an extra \. So your string would be

>>> print "\\bin\\example.exe"
\bin\example.exe

Comments

2

You need double backslashes \\ otherwise python thinks they are escape sequences:

print "\\bin\\example.exe"

Look here for more information on string literals and escape sequences.

Comments

1

escape the backslashes with backlashes

print "\\bin\\example.exe"

2 Comments

Thanks! All the comments and answers worked but you were first.
Look at @AER's answer. It is more comprehensive.

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.