Skip to content

Python String Escaping

Syntax

Escape sequences in Python use a backslash (\) to insert special characters or handle special situations in strings.

# Common escape sequences
newline = "This is the first line.\nThis is the second line."
tab = "Column1\tColumn2\tColumn3"
quote = "She said, \"Hello, World!\""
backslash = "This is a backslash: \\"

Example

# Creating a file path with escape sequences
file_path = "C:\\Users\\John\\Documents\\file.txt"
print(f"The file is located at: {file_path}")