escape sequence

In Python, an escape sequence is a series of characters that represents a special character or action in a string. They allow you to include characters in strings that are otherwise difficult or impossible to express directly, such as newlines, tabs, or quotes.

Escape sequences start with a backslash (\), followed by a character that has a special meaning. For instance, \n represents a newline, \t represents a tab, and \\ represents a literal backslash.

Using escape sequences can make your strings more readable and easier to manage, especially when dealing with special characters or formatting requirements.

Example

Here’s a quick example of using escape sequences in Python:

Python
>>> text = "Hello, Pythonistas!\nWelcome to Real Python!\tLet's learn Python!"
>>> print(text)
Hello, Pythonistas!
Welcome to Real Python! Let's learn Python!

When you run this code, you’ll see that \n creates a new line, and \t adds a tab space.

Tutorial

Strings and Character Data in Python

In this tutorial, you'll learn how to use Python's rich set of operators and functions for working with strings. You'll cover the basics of creating strings using literals and the str() function, applying string methods, using operators and built-in functions with strings, and more!

basics python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated April 14, 2025 • Reviewed by Leodanis Pozo Ramos