Escape Characters in Strings
Concepts covered: Strings
Some characters have no key you can press inside a string. A tab is one. A line break is another when it has to live in a string written with one pair of quotes, and so is a double quote inside a string that is itself delimited by double quotes. The backslash solves this. A backslash followed by 1 character is an escape sequence, and Python stores the character the pair stands for instead of the 2 symbols you typed.
| Escape | Stands for | Example | Printed |
|---|---|---|---|
| \n | line break | "a\nb" | a, then b on the next line |
| \t | tab | "a\tb" | a, a jump to the next tab stop, b |
| \" | double quote | "say \"hi\"" | say "hi" |
| \' | single quote | 'it\'s' | it's |
| \\ | one backslash | "C:\\temp" | C:\temp |
print("Line 1\nLine 2")
print("Name:\tAda")
print("She said \"hi\" and left")
print('it\'s done')
print("C:\\temp\\logs")Output:
Line 1 Line 2 Name: Ada She said "hi" and left it's done C:\temp\logs
The tab is the odd one out: it moves the cursor to the next tab stop, usually every 8 columns, rather than inserting a fixed number of spaces. Short values followed by \t therefore line up in the same column, which is handy for a quick table printed to the screen.
print("City\tCode")
print("Oslo\tOSL")
print("Lima\tLIM")Output:
City Code Oslo OSL Lima LIM
An escape sequence is 1 character
You type 2 symbols, but the string stores 1 character. "a\nb" has 3 characters, and "C:\\temp" has 7, with a single backslash sitting between the colon and the t. A loop that counts characters confirms both figures.
one = "a\nb"
count = 0
for ch in one:
count = count + 1
print(count)
path = "C:\\temp"
count = 0
for ch in path:
count = count + 1
print(count)Output:
3 7
Triple quotes write \n for you
The line breaks you typed inside triple quotes in the previous section are these same \n characters. A 2-line triple-quoted string and a string written with one pair of quotes and \n in the middle are equal, character for character. Choose triple quotes when the text is long enough that seeing its shape helps, and \n when the string is short or built from parts with +.
one = "a\nb"
two = """a
b"""
print(one == two)
print("Total: 40" + "\n" + "Paid: 40")Output:
True Total: 40 Paid: 40
Backslashes in a pasted Windows path
A backslash in text you paste from elsewhere, a Windows folder path for instance, is read as the start of an escape. "C:\new folder" contains \n, so the path breaks across 2 lines when printed and the letter n is gone. Doubling every backslash restores the intended characters.
print("C:\new folder")
print("C:\\new folder")Output:
C: ew folder C:\new folder
- Do
- Double a backslash that should appear in the output
- Use \n for a short 2-line string built with +
- Switch quote styles before reaching for \" or \'
- Avoid
- Paste a path with backslashes into a string unchanged
- Count an escape as 2 characters
- Expect \t to insert a fixed number of spaces
How many characters does the loop count?
s = "a\\b\n"
count = 0
for ch in s:
count = count + 1
print(count)- 6: every typed symbol counts as one
- 3: a, b and a line break, the backslashes vanish
- 5: a, 2 backslashes, b and a line break
- 4: a, 1 backslash, b and a line break
Several escapes in one string
Escapes can sit next to each other and next to ordinary letters in the same string. Python reads the string from left to right, and each time it meets a backslash it takes the backslash and the next character as one pair, stores the character the pair stands for, and carries on. So nothing stops a single string from holding a tab and a line break along with a quoted word.
card = "Artist:\t\"Abba\"\nPlays:\t12"
print(card)
count = 0
for ch in card:
count = count + 1
print(count)Output:
Artist: "Abba" Plays: 12 24
Between its quotes the source line has 29 typed symbols, and the loop counts 24 characters: each of the 5 escapes was typed as 2 symbols and stored as 1. The tab after Plays: puts 12 in the same column as the quoted name, because both labels end before the first tab stop.
About this lesson
This section is part of the Strings: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.