Write text with quotes and escape sequences, join and repeat strings, and count characters with len().
About 25 minFirst steps4 exercisesFree
You will be able to
Write strings that contain quote characters
Use the escape sequences \n, \t, \" and \\
Join strings with + and repeat them with *
Count the characters in a string with len()
Names, addresses, product descriptions, messages, labels on a receipt: most of what programs show people is text. In Python, a piece of text is a string, short for "string of characters". A character is a single letter, digit, space or symbol.
You've been writing strings since your first program. This lesson shows what to do when the text itself contains quotes, how to put special characters such as tabs and line breaks inside a string, and how to build longer strings from shorter ones.
Quotes inside text
A string starts and ends with matching quotes. That causes a problem when the text itself contains the same quote character: Python thinks the string has ended early.
The simplest fix is to use the other kind of quote around the string:
Try it: Change the first line to use single quotes around the whole string. Run it and read the error.
Escape sequences
Sometimes you need a character you can't type directly into a string, or a quote of the same kind as the ones around it. A backslash\ starts an escape sequence: the backslash and the character after it together stand for one special character.
Escape
Means
\n
a new line
\t
a tab (jumps to the next column)
\"
a double quote inside a double-quoted string
\'
a single quote inside a single-quoted string
\\
one backslash
The backslash itself is never printed; it only changes the meaning of the character after it.
Escape sequences in action
print("Line one\nLine two")print("Item\tPrice")print("Tea\t8")print("She said \"thank you\"")print("Folder: C:\\Reports")
Try it: Add a third line to the first string by putting another \n and some text before the closing quote.
Joining and repeating strings
The + operator joins two strings end to end. This is called concatenation. Python adds nothing between them, so you must include any spaces yourself.
The * operator repeats a string a number of times, which is handy for divider lines.
Try it: Change the divider to use = and make it 30 characters long.
+ and * mean different things for numbers and for strings. Compare these carefully:
5 + 3 is the number 8, but "5" + "3" is the string "53".
3 * 4 is 12, but "3" * 4 is "3333".
Python decides what + does by looking at the values on each side. It refuses to guess when you mix them: "Total: " + 5 stops with a TypeError, because you can't join text and a number with +. When you want to print text and a number together, separate them with a comma in print(), as you've been doing.
Text and a number can't be joined with +
print("Total: "+5)
Try it: Fix it in two different ways: write print("Total:", 5), and write print("Total: " + "5").
Counting characters with len()
len() is a function that tells you the length of a string: how many characters it contains. Spaces and punctuation count as characters too.
Guided practice · ●difficulty 1 of 5 · about 3 min
A sign with quotes
Print this shop sign exactly, including the apostrophe in the first line and the double quotes in the second:
Fatima's Flowers
Our motto: "Fresh every morning"
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Challenge · ●●difficulty 2 of 5 · about 5 min
A receipt banner
Noor Bookshop wants a banner at the top of its receipts, followed by a line showing how many characters the shop name has:
==============================
NOOR BOOKSHOP
==============================
Name length: 13
Requirements:
Each divider is 30= characters. Build it with string repetition (*), not by typing 30 characters.
Work out the 13 with len("NOOR BOOKSHOP").
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Edge cases · ●●difficulty 2 of 5 · about 4 min
A tab-separated stock table
Print this stock table for a stationery shop. Between the two columns on each line there is a single tab character (\t), not spaces:
Item Qty
Pens 120
Folders 45
Write the whole table with oneprint() call, using \n for the line breaks and \t for the tabs.
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Debugging · ●●difficulty 2 of 5 · about 4 min
Fix the broken file path
A report generator should print where it saved a file:
Saved to C:\new\tables\sales.txt
Instead the output is split over several lines and has strange gaps. Fix the string so that the path prints exactly as shown, backslashes included.
Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.
Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type
Output
Run your code to see its output, or Check to test it against the requirements.
The solution becomes available after you pass, or after all three hints and two checks.
Summary
A string is text inside matching quotes. Use the other kind of quote, or an escape, when the text contains quotes.
Escape sequences start with a backslash: \n new line, \t tab, \" quote, \\ backslash.
+ joins strings with nothing in between; * repeats a string.
len() counts the characters in a string, including spaces.
Text and numbers can't be joined with +; separate them with commas in print().