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

String Escape Sequencing in Python

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:

  1. \\ - Backslash (\)
  2. \' - Single quote (')
  3. \" - Double quote (")
  4. \n - Newline (line break)
  5. \t - Tab (horizontal tab)
  6. \r - Carriage return (used in Windows line endings)
  7. \b - Backspace
  8. \f - Form feed (used for page breaks in printers)
  9. \v - Vertical tab
  10. \a - Bell (used to trigger an alert sound on some systems)
  11. \ooo - Octal value (replace 'ooo' with an octal number, e.g., \123)
  12. \xhh - Hex value (replace 'hh' with a hexadecimal number, e.g., \x1A)
  13. \uHHHH - Unicode character with 16-bit hex value (replace 'HHHH' with a 4-digit hexadecimal number, e.g., \u2602)
  14. \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.

  1. Escape characters in Python string literals:

    • Description: Escape characters are used to include special characters in strings by using a backslash \.
    • Example Code:
      escaped_string = "This is an example of escape character: \n New line"
      
  2. Using escape sequences for special characters in Python:

    • Description: Escape sequences allow you to represent special characters like newline (\n), tab (\t), etc.
    • Example Code:
      special_string = "Tabbed \t text and new line \n Next line"
      
  3. Multiline strings and escape sequences in Python:

    • Description: Multiline strings can be created using triple-quotes (''' or """) and escape sequences can be used within them.
    • Example Code:
      multiline_string = '''This is a
      multiline
      string with newline.'''
      
  4. Double quotes vs. single quotes in Python escape sequences:

    • Description: Escape sequences work the same way in both double and single-quoted strings.
    • Example Code:
      double_quoted = "Double-quoted string with escape sequence: \n New line"
      single_quoted = 'Single-quoted string with escape sequence: \n New line'
      
  5. Raw strings and escape characters in Python:

    • Description: Raw strings (r"...") treat backslashes as literal characters, ignoring escape sequences.
    • Example Code:
      raw_string = r"This is a raw string with \n escaped characters"
      
  6. Escape sequences for newline and carriage return in Python:

    • Description: \n represents a newline character, and \r represents a carriage return.
    • Example Code:
      new_line = "This is a string with a newline character.\nNew line."
      carriage_return = "This is a string with a carriage return.\rReturn."
      
  7. Unicode escape sequences in Python strings:

    • Description: Unicode escape sequences (\u and \U) allow representing Unicode characters in strings.
    • Example Code:
      unicode_string = "Unicode character: \u03A9"  # Omega symbol