Multiline & Raw Strings
Python provides special string literals for multi-line content and for strings where backslash should not be treated as an escape character.
Multiline Strings (Triple Quotes)
poem = """Roses are red,
Violets are blue,
Python is awesome,
And so are you."""
print(poem)
sql = """
SELECT *
FROM users
WHERE active = 1
"""Raw Strings
# Without raw string (need double backslash)
path1 = "C:\\Users\\Alice\\file.txt"
# With raw string (backslash is literal)
path2 = r"C:\Users\Alice\file.txt"
# Useful for regex patterns
import re
pattern = r"\d{3}-\d{4}"
print(re.match(pattern, "555-1234"))Escape Sequences
print("Line1\nLine2") # newline
print("Tab\there") # tab
print("Say \"hi\"") # quotes
print("Back\slash") # backslash