Quoting a Backslash in a string in Python

avatar

Borislav Hadzhiev

Last updated: Sep 20, 2022

banner

Photo from Unsplash

Quoting a Backslash in a string in Python #

Alternate between single and double quotes to quote a backslash in a string, e.g. my_str = 'bobby "\\" hadz'. Backslash characters have a special meaning in Python, so they have to be escaped with a second backslash.

main.py
# ✅ quote a backslash in a string (escaping) my_str = 'bobby "\\" hadz' print(my_str) # 👉️ bobby "\" hadz # ------------------------------------- # ✅ quote a backslash in a string (raw string) my_str = r'bobby "\" hadz' print(my_str) # 👉️ bobby "\" hadz # ------------------------------------- # ✅ quote a backslash (str.replace()) my_str = 'bobby \\ hadz' print(my_str) # 👉️ bobby \ hadz my_str = my_str.replace('\\', '"\\"') print(my_str) # 👉️ bobby "\" hadz
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.

main.py
my_str = 'bobby "\\" hadz' print(my_str) # 👉️ bobby "\" hadz

Notice that we wrapped the string in single quotes and used double quotes to quote the backslash character.

You must alternate between single and double quotes, otherwise, you'd terminate the string prematurely.

Here is an example of quoting a backslash using single quotes.

main.py
my_str = "bobby '\\' hadz" print(my_str) # 👉️ bobby '\' hadz

If you need to include multiple backslashes next to one another, make sure to escape each.

main.py
my_str = 'bobby "\\\\" hadz' print(my_str) # 👉️ bobby "\\" hadz

Alternatively, you can use a raw string.

Quoting a Backslash in a string using a raw string #

Use a raw string to quote a backslash, e.g. my_str = r'bobby "\" hadz'. Strings that are prefixed with r are called raw strings and treat backslashes as literal characters.

main.py
my_str = r'bobby "\" hadz' print(my_str) # 👉️ bobby "\" hadz

We prefixed the string with r, so backslashes are treated as literal characters.

However, we still have to alternate between single and double quotes.

If you have a variable that stores a string containing a backslash and need to quote the backslash, use the str.replace() method.
main.py
my_str = 'bobby \\ hadz \\' print(my_str) # 👉️ bobby \ hadz \ my_str = my_str.replace('\\', '"\\"') print(my_str) # 👉️ bobby "\" hadz "\"

We used the str.replace() method to replace each occurrence of a backslash with a backslash wrapped in quotes.

If you need to quote only the first backslash in the string, pass a third argument to the replace() method.

main.py
my_str = 'bobby \\ hadz \\' print(my_str) # 👉️ bobby \ hadz \ my_str = my_str.replace('\\', '"\\"', 1) print(my_str) # 👉️ bobby "\" hadz \

The str.replace method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)

The method doesn't change the original string. Strings are immutable in Python.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.