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)

Python escape character

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

  • Escaping a single quote:
text = 'It\'s a beautiful day!'
print(text)  # Output: It's a beautiful day!
  • Escaping a double quote:
text = "She said, \"Hello, world!\""
print(text)  # Output: She said, "Hello, world!"
  • Using newline and tab escape characters:
text = "First line\n\tIndented second line"
print(text)
# Output:
# First line
#     Indented second line
  • Using a backslash:
path = "C:\\Users\\John\\Documents"
print(path)  # Output: C:\Users\John\Documents
  • Using octal and hexadecimal escape characters:
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.

  1. Common Escape Sequences in Python:

    • Escape sequences are special characters preceded by a backslash. Common ones include \n (newline), \t (tab), \' (single quote), \" (double quote), and \\ (backslash).
    # Example
    newline_example = "Line 1\nLine 2"
    tab_example = "Item\tQuantity"
    
  2. Using Escape Characters in Strings in Python:

    • Escape characters are used to represent special characters within strings.
    # Example
    escape_example = "This is an example of an escape sequence: \nNew Line"
    
  3. Escape Characters for Special Characters in Python:

    • Escape characters are used to include special characters within strings, such as quotes.
    # Example
    special_characters = "Special characters: \' \" \\"
    
  4. 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"
    
  5. Handling Special Characters with Escape Sequences in Python:

    • Escape sequences are crucial for handling special characters within strings.
    # Example
    special_characters = "Quotes: \' \""
    
  6. Backslash (\) as an Escape Character in Python:

    • The backslash (\) itself is used as an escape character.
    # Example
    backslash_example = "This is a backslash: \\"
    
  7. Raw Strings and Escape Characters in Python:

    • Raw strings (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"
    
  8. Escape Character Reference in Python:

    • Refer to the Python documentation for a comprehensive list of escape sequences.
    # Example
    escape_reference = "Check the Python docs for escape sequences: https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals"