Escape sequences in Python are used to insert special characters into strings that are otherwise difficult to include directly. These sequences start with a backslash (\
) followed by a specific character. Escape sequences enable the inclusion of characters such as newlines, tabs, and quotes within strings, allowing for more control over string formatting and display.
Escape Sequence | Description |
---|---|
\\ | Backslash |
\' | Single quote |
\" | Double quote |
\n | Newline |
\t | Horizontal tab |
\r | Carriage return |
\b | Backspace |
\f | Form feed |
\a | Bell/alert |
\v | Vertical tab |
\ooo | Character with octal value ooo |
\xhh | Character with hex value hh |
\N{name} | Character from Unicode database with name name |
\uXXXX | Character with 16-bit hex value XXXX |
\UXXXXXXXX | Character with 32-bit hex value XXXXXXXX |
\ followed by a newline | Continuation of a string literal over multiple lines |
# Example 1: Using escape sequences for formatting
name = "Alice"
age = 30
# Using escape sequences to format the output
print(f"Hello, {name}! You are {age} years old.")
print(f"Hello, {name}! You are {age} years old.\n") # \n adds a newline after the string
Explanation:
{}
."\n"
is the escape sequence for a newline character. It inserts a line break in the output.print
statement outputs: Hello, Alice! You are 30 years old.
on one line.print
statement outputs the same string but with a newline character at the end, resulting in the next output starting on a new line.# Example 2: Using escape sequences for special characters
path = "C:\\Users\\Alice\\Documents\\file.txt"
# Displaying the path using escape sequences
print(f"The file is located at: {path}")
Explanation:
path
contains a Windows file path."\\"
is the escape sequence for a backslash character \
. In strings, a single backslash \
is used to escape special characters, so to represent a literal backslash in a string, you need to use \\
.print(f"The file is located at: {path}")
will be: The file is located at: C:\Users\Alice\Documents\file.txt
.# Example: Using 5 different escape sequences
message = "Hello,\n\tPython!\nI hope you're learning a lot.\n\nThis is a backslash: \\\nAnd this is a double quote: \""
print(message)
Explanation:
\n
is used for a newline character. It moves the cursor to the next line.\t
is used for a tab character. It adds a tab space.\\\
is used to insert a literal backslash \
. In strings, a single backslash \
is used to escape special characters, so \\
is necessary to display a single backslash.\"
is used to insert a double quote character "
within a string that is already enclosed in double quotes. This escapes the character to prevent the string from being prematurely terminated.Output: When you run the code, the output will be:
Hello,
Python!
I hope you're learning a lot.
This is a backslash: \
And this is a double quote: "
In this example:
message
contains multiple lines of text separated by \n
for newlines and \t
for tabs.\\
to display a single backslash and \"
to include double quotes within a string enclosed by double quotes.