Write string literal without having to escape it in Python

avatar

Borislav Hadzhiev

Last updated: Sep 4, 2022

banner

Photo from Unsplash

Write string literal without having to escape it in Python #

Prefix the string with r to write a string literal without having to escape it, e.g. my_str = r'apple\nbanana\n\tkiwi'. When a string is prefixed with r, it treats backslashes as literal characters and escaping them is not necessary.

main.py
# 👇️ using raw string my_str = r'apple\nbanana\n\tkiwi' print(my_str) # 👉️ apple\nbanana\n\tkiwi # ---------------------------------------- # 👇️ using raw, formatted string literal variable = 'banana' my_str = fr'apple\n{variable}\n\tkiwi' print(my_str) # 👉️ apple\nbanana\n\tkiwi # ---------------------------------------- # 👇️ using triple-quoted raw string for multiline string multiline_str = r"""apple banana kiwi""" # apple # banana # kiwi print(multiline_str)

The first example uses a raw string to write a string literal without having to escape special characters.

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

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

main.py
variable = 'banana' my_str = fr'apple\n{variable}\n\tkiwi' print(my_str) # 👉️ apple\nbanana\n\tkiwi

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 we prefixed the string with fr and not just with f or r.

If you need to define a multiline string in which you don't have to escape special characters, use a triple-quoted raw string.
main.py
multiline_str = r"""apple \tbanana kiwi""" # apple # \tbanana # kiwi print(multiline_str)

If you need to interpolate variables in the string, prefix it with fr.

main.py
variable = 'banana' multiline_str = fr"""apple \t{variable} kiwi""" # apple # \tbanana # kiwi print(multiline_str)

Triple-quotes strings are very similar to basic strings that we declare using single or double quotes.

But they also enable us to:

  • use single and double quotes in the same string without escaping
  • define a multi-line string without adding newline characters

End of lines are automatically included in triple-quoted strings, so we don't have to add a newline character at the end.

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/Carl/Desktop/avatar.png' # 👇️ C:/Users/Bob/Desktop/avatar.png 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.