Python Escape Characters

Sharing is caring!

Last Updated on July 23, 2022 by Jay

The Python escape characters allow us to include special characters in strings. These escape characters usually start with a backslash \, then followed by a character. The escape characters can be helpful, yet they can also cause trouble sometimes. We’ll take a look at a few practical examples to understand how to use them.

List of Python Escape Characters

We’ll go through three practical escape character use cases here. The full list of escape characters can be found here if you want to learn more about them.

  • \n – new line
  • \\ – a literal backslash
  • \’ – a single quote
  • \” – a double quote

New Line Character

Sometimes we might want to print multiple lines of strings. We can achieve this by using one single print statement combined with the new line character \n to break a string into multiple lines.

print("this is a long sentence\nand we need to print it\non multiple lines!")
this is a long sentence
and we need to print it
on multiple lines!

Backslash

Double backslash \\ makes a literal backslash.

Because we have to start with a backslash when using escape characters, we can not simply use the backslash \ as a character in a string! A common example is a computer file path, for example: C:\Users\jay\Desktop\PythonInOffice\escape_character\eg.xlsx

If we type this folder path as a string in Python, we’ll get a SyntaxError.

print('C:\Users\jay\Desktop\PythonInOffice\escape_character\eg.xlsx'
)
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

One way to work around this problem is by adding a second backslash next to the existing backslashes, like the following:

print('C:\\Users\\jay\\Desktop\\PythonInOffice\\escape_character\\eg.xlsx')
C:\Users\jay\Desktop\PythonInOffice\escape_character\eg.xlsx

An alternative is to use the Python raw string or r-string. Check out this tutorial about r-string.

Single & Double Quote

One way to use quotation marks inside a string is by using different quotes for the string and the actual quotation part. As the below example shows, if we use single quotes for our string, then we’ll have to use double quotes for the quotation, and vice versa.

print('he said, "hello world"')
he said, "hello world"

print("python's awesome!")
python's awesome!

We’ll get an error if we use the same quotes for both places, for example:

print("he said, "hello world"")
SyntaxError: invalid syntax

print('python's awesome!')
SyntaxError: invalid syntax

The escape character can help us with this situation too. By placing a backslash right in front of a quotation mark, we turn it into a literal quotation mark! And as our examples show below, it works with either single or double quotes.

print("he said, \"hello world\"")
he said, "hello world"

print('python\'s awesome!')
python's awesome!

Additional Resources

Python Raw String r-string

Python F-String Formatting

Leave a Reply

Your email address will not be published. Required fields are marked *