Kodeclik Logo

Learn More

Fall 2025

Kodeclik Blog

Python Escape Characters

The term “escape character” sounds like we are doing something surreptitiously but in reality it is a simple idea.

What is an escape character?

Let us suppose we want to print ‘Hello World’ in python. You will do:

This will give the output:

But what if you also wanted the quotes, before and after your string? How do you tell Python that you mean quotes literally and not just as denoting the beginning and end of a string? This is where escape characters come in!

You should write your program as:

Note that the string inside the print statement still has a beginning quote and end quote. But in between these quotes, there are other quotes prefixed by a backslash. This creates the escape character (\’) which means to print a quote literally. The output will be:

as desired.

When should you use an escape character?

Escape characters are essential when you need to include special characters in Python strings that would otherwise cause syntax errors or unintended behavior.

Example 1: Including Quotes in Strings

As mentioned earlier, the most common use case for escape characters is when your string contains quotes that match the enclosing quotes. You escape them using \' or \":

The output will be:

Example 2: Adding Newlines or Tabs

Use \n for newlines and \t for tabs to format text output. For instance:

The output will be:

Example 3: Including Backslashes

To display a backslash (\), you need to escape it with another backslash (\\):

The output is:

Example 4: Using Unicode Characters

Escape sequences like \u and \U allow you to include Unicode characters in strings:

The output is:

What other escape characters are available?

Below is a printing of escape characters in Python that you can use as a reference:

The output will be as expected, a grid of 4x4 characters:

Conclusion

By mastering escape characters, you can handle complex string formatting tasks with ease and precision.

Enjoy this blogpost? Want to learn Python with us? Sign up for 1:1 or small group classes.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2025. All rights reserved.