Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
In Python, escape sequences are used to represent special characters within string literals. These special characters include newline, tab, backslash, and others. An escape sequence starts with a backslash (\
) followed by one or more characters.
Here are some common escape sequences in Python:
\\
- Backslash (\
)\'
- Single quote ('
)\"
- Double quote ("
)\n
- Newline (line break)\t
- Tab (horizontal tab)\r
- Carriage return (used in Windows line endings)\b
- Backspace\f
- Form feed (used for page breaks in printers)\v
- Vertical tab\a
- Bell (used to trigger an alert sound on some systems)\ooo
- Octal value (replace 'ooo' with an octal number, e.g., \123
)\xhh
- Hex value (replace 'hh' with a hexadecimal number, e.g., \x1A
)\uHHHH
- Unicode character with 16-bit hex value (replace 'HHHH' with a 4-digit hexadecimal number, e.g., \u2602
)\UHHHHHHHH
- Unicode character with 32-bit hex value (replace 'HHHHHHHH' with an 8-digit hexadecimal number, e.g., \U0001F600
)Here are some examples of escape sequences used in Python strings:
print("This is a backslash: \\") # Output: This is a backslash: \ print("This is a single quote: \' and this is a double quote: \"") # Output: This is a single quote: ' and this is a double quote: " print("This is a newline character:\nThis is the next line.") # Output: # This is a newline character: # This is the next line. print("This is a tab character:\tThis text is indented.") # Output: This is a tab character: This text is indented. print("This is a backspace character: ab\bcd") # Output: This is a backspace character: acd print("This is a unicode character: \u2602") # Output: This is a unicode character: ☂ print("This is a unicode character: \U0001F600") # Output: This is a unicode character: 😀
Remember that when using escape sequences in a string, it's important to use a raw string (prefix the string with an 'r' or 'R') if you don't want the escape sequences to be interpreted. For example:
print(r"This is a raw string: \\n") # Output: This is a raw string: \\n
In this example, the raw string preserves the literal backslash and 'n' characters instead of interpreting them as a newline escape sequence.
Escape characters in Python string literals:
\
.escaped_string = "This is an example of escape character: \n New line"
Using escape sequences for special characters in Python:
\n
), tab (\t
), etc.special_string = "Tabbed \t text and new line \n Next line"
Multiline strings and escape sequences in Python:
'''
or """
) and escape sequences can be used within them.multiline_string = '''This is a multiline string with newline.'''
Double quotes vs. single quotes in Python escape sequences:
double_quoted = "Double-quoted string with escape sequence: \n New line" single_quoted = 'Single-quoted string with escape sequence: \n New line'
Raw strings and escape characters in Python:
r"..."
) treat backslashes as literal characters, ignoring escape sequences.raw_string = r"This is a raw string with \n escaped characters"
Escape sequences for newline and carriage return in Python:
\n
represents a newline character, and \r
represents a carriage return.new_line = "This is a string with a newline character.\nNew line." carriage_return = "This is a string with a carriage return.\rReturn."
Unicode escape sequences in Python strings:
\u
and \U
) allow representing Unicode characters in strings.unicode_string = "Unicode character: \u03A9" # Omega symbol