How to print a backslash in Python

avatar

Borislav Hadzhiev

Last updated: Sep 4, 2022

banner

Photo from Unsplash

Print a backslash in Python #

To print a backslash:

  1. Use a second backslash character to escape each backslash in the string.
  2. Use the print() function to print the result.
  3. The backslash character has a special meaning, so it has to be escaped with a second backslash.
main.py
# ✅ Print a single backslash (escaping) my_str = 'Alice\\Bob\\Carl' print(my_str) # 👉️ Alice\Bob\Carl # -------------------------------------- # ✅ print double backslash (escaping) my_str = 'Alice\\\\Bob\\\\Carl' print(my_str) # 👉️ Alice\\Bob\\Carl # -------------------------------------- # ✅ print single backslash (raw string) my_str = r'Alice\Bob\Carl' print(my_str) # 👉️ Alice\Bob\Carl # -------------------------------------- # ✅ print double backslash (raw string) my_str = r'Alice\\Bob\\Carl' print(my_str) # 👉️ Alice\\Bob\\Carl

The first example uses a second backslash to escape each backslash character in the string.

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 \ as a literal character.

main.py
my_str = 'Alice\\Bob\\Carl' print(my_str) # 👉️ Alice\Bob\Carl

If you need to print two backslash characters next to one another, use four backslashes.

main.py
my_str = 'Alice\\\\Bob\\\\Carl' print(my_str) # 👉️ Alice\\Bob\\Carl

Alternatively, you can use a raw string.

Print a backslash using a raw string #

Prefix the string with r to print a backslash, e.g. print(r'Alice\Bob\Carl'). When a string is prefixed with r, it treats backslashes as literal characters and escaping them is not necessary.

main.py
my_str = r'Alice\Bob\Carl' print(my_str) # 👉️ Alice\Bob\Carl my_str = r'Alice\\Bob\\Carl' print(my_str) # 👉️ Alice\\Bob\\Carl
Strings that are prefixed with r are called raw strings and treat backslashes as literal characters.

There is no need to escape backslash characters when using a raw string.

If you need to interpolate variables in a raw string, use a formatted string literal.

main.py
name = 'Bob' my_str = fr'Alice\{name}\Carl' print(my_str) # 👉️ Alice\Bob\Carl
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

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

Notice that the string is prefixed with fr and not just with f.

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

main.py
file_name = 'C:/Users/Alice/Desktop/example.txt' # 👇️ C:/Users/Bob/Desktop/example.txt print(file_name)

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

Backslash characters have a special meaning in Python, so to treat them as literal characters, we have to:

  • escape each backslash with a second backslash
  • prefix the string with r to mark it as a raw string
  • use forward slashes in the place of backslashes in a path
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.