Add backslash to string python

Python treats \ in literal string in a special way.
This is so you can type '\n' to mean newline or '\t' to mean tab
Since '\&' doesn't mean anything special to Python, instead of causing an error, the Python lexical analyser implicitly adds the extra \ for you.

Really it is better to use \\& or r'\&' instead of '\&'

The r here means raw string and means that \ isn't treated specially unless it is right before the quote character at the start of the string.

In the interactive console, Python uses repr to display the result, so that is why you see the double '\'. If you print your string or use len(string) you will see that it is really only the 2 characters

Some examples

>>> 'Here\'s a backslash: \\'
"Here's a backslash: \\"
>>> print 'Here\'s a backslash: \\'
Here's a backslash: \
>>> 'Here\'s a backslash: \\. Here\'s a double quote: ".'
'Here\'s a backslash: \\. Here\'s a double quote: ".'
>>> print 'Here\'s a backslash: \\. Here\'s a double quote: ".'
Here's a backslash: \. Here's a double quote ".

To Clarify the point Peter makes in his comment see this link

Unlike Standard C, all unrecognized escape sequences are left in the string unchanged, i.e., the backslash is left in the string. (This behavior is useful when debugging: if an escape sequence is mistyped, the resulting output is more easily recognized as broken.) It is also important to note that the escape sequences marked as “(Unicode only)” in the table above fall into the category of unrecognized escapes for non-Unicode string literals.

Concatenate a string with a backslash in Python #

Use a second backslash to concatenate a string with a backslash in Python, e.g. result_1 = 'one\\' + 'two'. The backslash \ character has a special meaning, so it has to be escaped with a second backslash.

Copied!

result_1 = 'one\\' + 'two' print(result_1) # 👉️ 'one\two' result_2 = r'one\two' print(result_2) # 👉️ 'one\two' first = 'one' second = 'two' result_3 = rf'{first}\{second}' print(result_3) # 👉️ 'one\two'

The first example uses a second backslash to escape a backslash character.

The backslash \ character has a special meaning in Python - it is used as an escape character (e.g. \n or \t).

By adding a second backslash, we treat the backslash (\) as a literal character.

Copied!

file_name = 'C:\\Users\\Bob\\Desktop\\employees.csv' # 👇️ C:\Users\Bob\Desktop\employees.csv print(file_name)

Similarly, if you need to have 2 backslashes next to one another, you would have to use 4 backslashes.

Copied!

result_1 = 'one\\\\' + 'two' print(result_1) # 👉️ 'one\\two'

An alternative solution is to prefix the string literal with r to mark it as a raw string.

Copied!

file_name = r'C:\Users\Bob\Desktop\employees.csv' # 👇️ C:\Users\Bob\Desktop\employees.csv print(file_name)

Strings that are prefixed with r are called raw strings and treat backslashes as literal characters.

If you are constructing a path, e.g. to a directory or a file, you can use forward slashes instead of backslashes.

Copied!

file_name = 'C:/Users/Bob/Desktop/employees.csv' # 👇️ C:/Users/Bob/Desktop/employees.csv print(file_name)

A forward slash can be used in the place of a backslash when you need to specify a path.

You can also use a raw, formatted-string literal to treat a backslash as a literal character.

Copied!

first = 'one' second = 'two' result_3 = rf'{first}\{second}\three' print(result_3) # 👉️ 'one\two\three'

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Copied!

my_str = 'is subscribed:' my_bool = True result = f'{my_str} {my_bool}' print(result) # 👉️ is subscribed: True

Make sure to wrap expressions in curly braces - {expression}.

By prefixing the string with r as well as f, we mark it as both - a raw string and a formatted-string literal.

Since backslash characters have a special meaning in Python, we need to treat them as a literal character by:

  • escaping each backslash with a second backslash
  • prefixing the string with r to mark it as a raw string
  • using forward slashes in the place of backslashes in a path

How do you add a backslash to a string in Python?

Use two backslashes to represent a backslash Use the syntax "\\" within the string literal to represent a single backslash.

How do you add a backslash to a string?

Solution 1. The backslash ( "\" ) character is a special escape character used to indicate other special characters such as new lines ( \n ), tabs ( \t ), or quotation marks ( \" ). If you want to include a backslash character itself, you need two backslashes or use the @ verbatim string: "\\Tasks" or @"\Tasks" .

How do I print a single backslash in Python?

For completeness: A backslash can also be escaped as a hex sequence: "\x5c" ; or a short Unicode sequence: "\u005c" ; or a long Unicode sequence: "\U0000005c" . All of these will produce a string with a single backslash, which Python will happily report back to you in its canonical representation - '\\' .

How do you quote a backslash in Python?

You can put a backslash character followed by a quote ( \" or \' ). This is called an escape sequence and Python will remove the backslash, and put just the quote in the string.