Special Characters and Multi-line Text

Escape characters

A backslash \ gives the next character a special meaning:

WriteYou get
\na new line
\ta tab (a wide space)
\"a double quote inside a string
\\one backslash
print("Line one\nLine two")
print("Name:\tAsha")
print("She said \"hi\"")

Output

Line one
Line two
Name:	Asha
She said "hi"

Text across several lines

Triple quotes """ let a string stretch over many lines. The line breaks are kept:

poem = """Roses are red,
Python is cool,
I write code
After school."""
print(poem)

Output

Roses are red,
Python is cool,
I write code
After school.

Tip: On Windows, file paths contain backslashes. Write them with a leading r – a raw string – so the backslashes stay as they are: r"C:\new\files".

HOME LEARN COMMUNITY DASHBOARD