Escape Sequence in strings - Python

Table Of Content
- 1. **\n** - Newline
- Output:
- Hello
- World
- 2. **\t** - Tab
- Output:
- Hello World
- 3. **\\** - Backslash
- Output:
- C:\Users\Public
- 4. **\'** - Single Quote
- Output:
- He said, 'Hello'
- 5. **\"** - Double Quote
- Output:
- He said, "Hello"
- 6. **\a** - Bell
- 7. **\b** - Backspace
- Output:
- Hello
- 8. **\r** - Carriage Return
- Output:
- World
- 9. **\f** - Form Feed
- 10. **\v** - Vertical Tab
- 11. **\000** - Null
- 12. **\xhh** - Hexadecimal
- 13. **\uhhhh** - Unicode Character
- 14. **\Uhhhhhhhh** - Unicode Character (Extended)
- 1. **\o** - Octal Escape Sequence
- 2. **\x** - Hexadecimal Escape Sequence (Revisited)
- 3. **\u** - Unicode Escape Sequence (Revisited)
- 4. **\U** - Extended Unicode Escape Sequence (Revisited)
- 5. **\N name** - Unicode Character Name
- 6. **\0** - Null Character (Revisited)
- 7. **\n** - Line Feed (LF)
- 8. **\r** - Carriage Return (CR)
- 9. **\r\n** - Carriage Return + Line Feed (CRLF)
- 10. **\v** - Vertical Tab (Revisited)
- 11. **\f** - Form Feed (Revisited)
- 12. **\a** - Bell (Alert) (Revisited)
- 13. **\b** - Backspace (Revisited)
- 14. **\e** or **\033** - Escape Character
Escape sequences in Python are used within string literals to represent special characters or to include characters that would otherwise have a special meaning. Here are some of the most commonly used escape sequences:
1. \n - Newline
- Represents a line break.
print("Hello\nWorld")
# Output:
# Hello
# World
2. \t - Tab
- Represents a horizontal tab.
print("Hello\tWorld")
# Output:
# Hello World
3. \ - Backslash
- Represents a backslash itself since backslash is used to denote escape sequences.
print("C:\\Users\\Public")
# Output:
# C:\Users\Public
4. ' - Single Quote
- Used to include a single quote inside a string defined with single quotes.
print('He said, \'Hello\'')
# Output:
# He said, 'Hello'
5. " - Double Quote
- Used to include a double quote inside a string defined with double quotes.
print("He said, \"Hello\"")
# Output:
# He said, "Hello"
6. \a - Bell
- Produces a bell (alert) sound on systems that support it.
print("\aThis will make a beep sound on some systems")
7. \b - Backspace
- Moves the cursor one position back.
print("Hel\blo")
# Output:
# Hello
8. \r - Carriage Return
- Moves the cursor to the beginning of the line.
print("Hello\rWorld")
# Output:
# World
9. \f - Form Feed
- Moves the cursor to the start of the next page (not commonly used in modern contexts).
print("Page 1\fPage 2")
10. \v - Vertical Tab
- Represents a vertical tab (rarely used).
print("Line 1\vLine 2")
11. \000 - Null
- Represents the null character (ASCII value 0).
print("Null character:\000")
12. \xhh - Hexadecimal
- Represents a character with the ASCII value given by the two hexadecimal digits hh.
print("\x48\x65\x6c\x6c\x6f") # Prints "Hello"
13. \uhhhh - Unicode Character
- Represents a Unicode character with the code point given by the four hexadecimal digits
hhhh
.
print("\u0048\u0065\u006c\u006c\u006f") # Also prints "Hello"
14. \Uhhhhhhhh - Unicode Character (Extended)
- Represents a Unicode character with the code point given by the eight hexadecimal digits
hhhhhhhh
.
print("\U0001F600") # Prints a smiling emoji
These escape sequences are fundamental for formatting output, handling special characters in strings, and working with various text encodings. They allow you to include characters that would otherwise be interpreted differently or to format your output in specific ways.
Yes, those are the most commonly used escape sequences in Python. Here's a quick recap:
- \n: Newline
- \t: Tab
- \: Backslash
- ': Single quote
- ": Double quote
- \a: Bell (alert)
- \b: Backspace
- \r: Carriage return
- \f: Form feed
- \v: Vertical tab
- \000: Null character
- \xhh: Hexadecimal character
- \uhhhh: Unicode character (4 hex digits)
- \Uhhhhhhhh: Unicode character (8 hex digits)
These cover most scenarios where you need to represent special characters or control sequences within strings. If you're working with more advanced text processing or specific character encodings, you might occasionally encounter or need to use these less common sequences:
- \N name: Unicode character by name (e.g.,
\N{Copyright Sign}
for ©)
However, for everyday programming in Python, the list provided earlier will suffice for almost all your needs regarding escape sequences. If you have any more questions or need further clarification, feel free to ask!
While the previously mentioned escape sequences cover most common use cases, there are a few more specialized or less commonly used escape sequences in Python:
1. \o - Octal Escape Sequence
- Represents a character with the ASCII value given by up to three octal digits
o
.
print("\042Hello\042") # Prints '"Hello"'
2. \x - Hexadecimal Escape Sequence (Revisited)
- While mentioned before, it's worth noting that you can use any number of hexadecimal digits following
\x
, but only the first two are used.
print("\x48\x65\x6c\x6c\x6f") # "Hello"
3. \u - Unicode Escape Sequence (Revisited)
- As mentioned,
\u
followed by exactly four hexadecimal digits represents a Unicode character.
print("\u0048\u0065\u006c\u006c\u006f") # "Hello"
4. \U - Extended Unicode Escape Sequence (Revisited)
\U
followed by exactly eight hexadecimal digits represents a Unicode character.
print("\U0001F600") # Smiling emoji
5. \N name - Unicode Character Name
- Represents a Unicode character by its name.
print("\N{Copyright Sign}") # Prints ©
6. \0 - Null Character (Revisited)
- While mentioned, it's worth emphasizing that
\0
represents the null character, which is often used in C-style string terminations or in certain binary data formats.
print("Null character:\0") # Note: This might not display anything in many terminals
7. \n - Line Feed (LF)
- Although commonly used for newlines, it's worth noting that in some systems (like Unix/Linux),
\n
is used as the line ending.
8. \r - Carriage Return (CR)
- In some systems (like macOS before OS X),
\r
was used for line endings.
9. \r\n - Carriage Return + Line Feed (CRLF)
- Used in Windows for line endings.
10. \v - Vertical Tab (Revisited)
- Although rare,
\v
can be used in certain text formatting scenarios or when dealing with legacy systems.
11. \f - Form Feed (Revisited)
- While not commonly used in modern contexts,
\f
was historically used in printers or when dealing with pagination in text.
12. \a - Bell (Alert) (Revisited)
- Although mentioned, it's worth noting that
\a
might not produce any sound on all systems or terminals.
13. \b - Backspace (Revisited)
- While
\b
moves the cursor back, it might not always behave as expected in all environments, especially in GUI applications or web browsers.
14. \e or \033 - Escape Character
- Often used in terminal applications for ANSI escape codes, which can control cursor movement, text color, etc.
print("\033[31mRed text\033[0m") # Prints "Red text" in red.
These additional sequences are less frequently used but can be crucial in specific applications, especially those dealing with low-level text manipulation, terminal interfaces, or when working with legacy systems and data formats.