Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
In this tutorial, we'll learn about escape characters in Python. Escape characters are used to represent special characters in strings that cannot be typed directly or have a specific meaning in the language syntax.
An escape character is represented by a backslash \
, followed by the character to be escaped. Here are some common escape characters:
\\
: Backslash\'
: Single quote\"
: Double quote\n
: Newline\t
: Tab\b
: Backspace\r
: Carriage return\f
: Form feed\v
: Vertical tab\ooo
: Octal value (where ooo
is a 3-digit octal number)\xhh
: Hexadecimal value (where hh
is a 2-digit hexadecimal number)Examples
text = 'It\'s a beautiful day!' print(text) # Output: It's a beautiful day!
text = "She said, \"Hello, world!\"" print(text) # Output: She said, "Hello, world!"
text = "First line\n\tIndented second line" print(text) # Output: # First line # Indented second line
path = "C:\\Users\\John\\Documents" print(path) # Output: C:\Users\John\Documents
octal_text = "\101\102\103" hex_text = "\x41\x42\x43" print(octal_text) # Output: ABC print(hex_text) # Output: ABC
Raw strings
In some cases, you may want to use a string that ignores escape characters. You can use raw strings by prefixing the string with an r
or R
. In a raw string, escape characters are treated as literal characters.
raw_text = r"C:\Users\John\Documents" print(raw_text) # Output: C:\Users\John\Documents
Note that in a raw string, you cannot use an escape sequence for a single backslash at the end of the string, as it would escape the closing quote. In this case, you can use a double backslash and slice the string.
raw_text_with_backslash = r"C:\Users\John\Documents\\"[:-1] print(raw_text_with_backslash) # Output: C:\Users\John\Documents\
In summary, escape characters in Python allow you to represent special characters within strings. By using a backslash followed by the character to be escaped, you can include characters that are otherwise difficult to type or have special meaning in the language syntax. Raw strings provide a convenient way to treat escape characters as literal characters when needed.
Common Escape Sequences in Python:
\n
(newline), \t
(tab), \'
(single quote), \"
(double quote), and \\
(backslash).# Example newline_example = "Line 1\nLine 2" tab_example = "Item\tQuantity"
Using Escape Characters in Strings in Python:
# Example escape_example = "This is an example of an escape sequence: \nNew Line"
Escape Characters for Special Characters in Python:
# Example special_characters = "Special characters: \' \" \\"
Newline and Tab Characters in Python Strings:
\n
represents a newline character, and \t
represents a tab character.# Example multiline_text = "Line 1\nLine 2\nLine 3" tabbed_text = "Name:\tJohn\nAge:\t25"
Handling Special Characters with Escape Sequences in Python:
# Example special_characters = "Quotes: \' \""
Backslash (\
) as an Escape Character in Python:
\
) itself is used as an escape character.# Example backslash_example = "This is a backslash: \\"
Raw Strings and Escape Characters in Python:
r''
) treat backslashes as literal characters and are useful for regex patterns.# Example raw_string_example = r"This is a raw string: C:\path\to\file"
Escape Character Reference in Python:
# Example escape_reference = "Check the Python docs for escape sequences: https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals"