You need to escape your backslash by preceding it with, yes, another backslash:
print["\\"]
And for versions prior to Python 3:
print "\\"
The \
character is called an escape character, which interprets the character following it differently. For example, n
by itself is simply a letter, but when you precede it with a backslash, it becomes \n
, which is the newline character.
As you can probably guess, \
also needs to be escaped
so it doesn't function like an escape character. You have to... escape the escape, essentially.
See the Python 3 documentation for string literals.
Print a backslash in Python #
To print a backslash:
- Use a second backslash character to escape each backslash in the string.
- Use the
print[]
function to print the result. - The backslash character has a special meaning, so it has to be escaped with a second backslash.
Copied!
# ✅ 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.
Copied!
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.
Copied!
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.
Copied!
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.
Copied!
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.
Copied!
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