SyntaxStudy
Sign Up
Python Multiline & Raw Strings
Python Beginner 8 min read

Multiline & Raw Strings

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
Example
haiku = """An old silent pond
A frog jumps into the pond
Splash! Silence again."""
print(haiku)
path = r"C:
ew_folder	est.txt"
print(path)  # backslashes are literal
Pro Tip

Raw strings cannot end with an odd number of backslashes — r"C:\" is valid but r"C:\" is a syntax error.