How can I put an actual backslash in a string literal (not use it for an escape sequence)?

Question:

I have this code:

import os
path = os.getcwd()
final = path +'xulrunner.exe ' + path + 'application.ini'
print(final)

I want output like:

C:Usersmexulrunner.exe C:Usersmeapplication.ini

But instead I get an error that looks like:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated xXX escape

I don’t want the backslashes to be interpreted as escape sequences, but as literal backslashes. How can I do it?


Note that if the string should only contain a backslash – more generally, should have an odd number of backslashes at the end – then raw strings cannot be used. Please use How can I get a string with a single backslash in it? to close questions that are asking for a string with just a backslash in it. Use How to write string literals in python without having to escape them? when the question is specifically about wanting to avoid the need for escape sequences.

Asked By: esafwan

||

Answers:

To answer your question directly, put r in front of the string.

final= path + r'xulrunner.exe ' + path + r'application.ini'

But a better solution would be os.path.join:

final = os.path.join(path, 'xulrunner.exe') + ' ' + 
         os.path.join(path, 'application.ini')

(the backslash there is escaping a newline, but you could put the whole thing on one line if you want)

I will mention that you can use forward slashes in file paths, and Python will automatically convert them to the correct separator (backslash on Windows) as necessary. So

final = path + '/xulrunner.exe ' + path + '/application.ini'

should work. But it’s still preferable to use os.path.join because that makes it clear what you’re trying to do.

Answered By: David Z

You can escape the slash. Use \ and you get just one slash.

Answered By: avacariu

You can escape the backslash with another backslash (\), but it won’t look nicer. To solve that, put an r in front of the string to signal a raw string. A raw string will ignore all escape sequences, treating backslashes as literal text. It cannot contain the closing quote unless it is preceded by a backslash (which will be included in the string), and it cannot end with a single backslash (or odd number of backslashes).

Answered By: Einsteinium

Another simple (and arguably more readable) approach is using string raw format and replacements like so:

import os
path = os.getcwd()
final = r"{0}xulrunner.exe {0}application.ini".format(path)
print(final)

or using the os path method (and a microfunction for readability):

import os

def add_cwd(path):
    return os.path.join( os.getcwd(), path )

xulrunner = add_cwd("xulrunner.exe")
inifile = add_cwd("application.ini")
# in production you would use xulrunner+" "+inifile
# but the purpose of this example is to show a version where you could use any character
# including backslash
final = r"{} {}".format( xulrunner, inifile )
print(final)
Answered By: Chris Rudd
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.