Learn about escape codes in python

Written by Jacqueline Lim

What will I be covering today?

I will be covering the data types that are in Python, which can consist of the normal data types, like int, str, boolean, float, and list, and also the escape codes.

The normal data types were covered in this post: Learning about data types

So I will be covering only the escape codes in this post.

What are escape codes?

Escape codes are a sequence of characters that are used to input characters that are either difficult to type out as part of a literal or that a syntax error will occur if not treated specially, it is used to insert characters that are illegal in a string.

What are the types of escape codes?

The types of escape codes are:

  • Backslash (\\)
  • Single quote (\')
  • Double quote (\")
  • Newline characters (\n)
  • Tab character (\t)
  • Backspace character (\b)
  • Ignore end of lines (\)

How can these escape codes be used?

They can be used in a sentence to separate the words so that they can be displayed correctly, like for a sentence in single quotes(' '), the single quote escape code (\') will help the programming language to understand that the sentence is not ending due to the backslash () at the front.

Here's how to use the backslash and single quote escape codes:

In the example below, there will be a sentence, and the backslash (\\) and single quote (\') escape codes will be used, the single quote will insert the quote without ending the sentence and the backslash will just insert a backslash.

print('I\'m home and I have work \\ How are you?')
#output: I'm home and I have work \ How are you?

Here's how to use the double quote and newline escape codes:

In the example below, there will be a sentence (x), the double quote (\") and newline (\n) escape codes will be used, the double-quote will insert the quote (") without ending the sentence and the newline will place the next part of the sentence on a new line.

x = "This is a good \" Harry Potter\" book, right? \n
      Yes I think so as well"
print(x)
#output: This is a good "Harry Potter" book, right?
#Yes I think so as well

Here's how to use the tab and backspace escape codes:

In the example below, there will be a sentence, the tab (\t) and backspace (\b) escape codes will be used, the tab will indent the words and the backspace will take away the space.

y = "This is a heart-warming show, isn't it? \b Oh? \t Really now"
#output: This is a heart-warming show, isn't it? Oh?    Really now

Here's how to use the ignore the end of line escape character:

In the example below, there will be a sentence (v), the ignore end of character (\) will then be used for ignoring the line break and treat the following line as if it is continued on the current line.

v = "This is a new sentence \
This is fun."
#output: This is a new sentence. This is fun.

Conclusion

While I was researching escape codes in Python, I realized how they can be used to structure a sentence correctly in Python so that the output will print out the exact sentence that I want to print.

Use escape characters to your advantage and print out the statements for easier reading!