DevAcademia
C++C#CPythonJava
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz

Loading Python tutorial…

Loading content
Python FundamentalsTopic 19 of 77
←PreviousPrevNextNext→

Escape Characters

Common Escape Sequences

Escape sequences allow you to include special characters inside strings that would otherwise be hard to represent. The most common ones are shown below.

EscapeDescriptionExample
\nNew lineprint("Line1\nLine2") # Line1 then Line2 on next line
\tTabprint("Name:\tAlice") # Adds a horizontal tab
\\Backslashprint("C:\\path") # Prints C:\path
\"Double quoteprint("He said \"Hi\"") # He said "Hi"
\'Single quoteprint('Don\'t') # Don't
\rCarriage returnprint("Overwrite\rNew") # 'Newwrite'
\bBackspaceprint("Hel\blo") # 'Hlo'

Raw Strings

Prefixing a string with 'r' (or 'R') creates a raw string, where backslashes are treated as literal characters instead of escape sequences. This is especially useful for file paths and regular expressions.

Example
# Regular vs raw strings
print("C:\\Users\\Alice")  # C:\Users\Alice
print(r"C:\Users\Alice")   # C:\Users\Alice

# Useful for regex patterns
import re
pattern = r"\d+"  # Matches one or more digits
Output
C:\Users\Alice
C:\Users\Alice

Unicode Escapes

Python strings are Unicode by default. Escape sequences can represent Unicode characters by name or by code point. Byte strings use hexadecimal escape sequences for binary data.

Example
# Unicode characters
print("\u03A9")       # Greek Omega: Ω
print("\N{SNOWMAN}")  # Unicode by name: ☃
print("\U0001F600")   # Full code point: 😀

# Byte strings
byte_str = b"\x48\x65\x6C\x6C\x6F"  # Represents b'Hello'
print(byte_str)
Output
Ω
☃
😀
b'Hello'
Test your knowledge: Escape Characters
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Python FundamentalsTopic 19 of 77
←PreviousPrevNextNext→