Strings & Quotes
A string is text — a word, a sentence, even a whole poem — written inside quotes. Python lets you use three kinds of quotes. At first that looks like too many. But one day an apostrophe will break your program, and then you will see why.
1Text inside quotes
Anything inside quotes is text, and Python calls text a string. Letters, digits, spaces, punctuation marks and even emoji can all go inside. The only rule is that it sits inside the quotes.
That last one is worth a second look. It printed 42, but because of the quotes it is text, not a number.
'42' with quotes is a string. 42 without quotes is a number. They look the same when printed, but you can do maths with one and not the other. The quotes make all the difference.2Why are there three kinds of quotes?
Because sometimes the text itself has a quote inside it. Python finds the end of your text by looking for the next matching quote. So if your text already contains that same quote mark, Python stops in the wrong place and the line breaks.
Try it below. Pick a message, wrap it in the wrong quote, and see it break. Then find the quote that works.
An apostrophe — the most common mistake.
Python treats the apostrophe in It's as the closing quote. So the string ends at It, and the leftover part — s a beautiful day — makes no sense to Python.
3The rule you just found
Use these most of the time. They work for normal text.
'He left at noon'Use these when your text has an apostrophe in it, like It's or don't.
"It's here"Use these when your text goes over more than one line. They also allow both other quote marks inside.
'''Two\nlines'''" marks in it? Use single quotes.4The apostrophe problem
This one is worth slowing down for, because it will happen to you. Type it at the prompt and Python complains:
Here is why. Python starts the string at the first quote. Then it reaches the apostrophe in It's and thinks “that is the closing quote, the string ends here”. So the string is just It. The rest — s a beautiful day' — is left over, and Python cannot make sense of it. Unterminated means a string was opened and never closed.
The fix is not to remove the apostrophe. The fix is to change the quotes around it:
Try both at the prompt below. Then swap the quotes around — put single quotes around It's — and watch Python break.
5Triple quotes for many lines
A single-quoted or double-quoted string must start and end on the same line. If you press Enter in the middle of one, Python gives an error. When you really need several lines — an address, a poem, a menu — use triple quotes. You can use either ''' or """.
... dots? When you press Enter in the middle of a triple-quoted string, Python knows you have not finished. Instead of the usual >>>, it shows ... to say “carry on, I am still listening”. You do not type those dots. Python prints them. The moment you close the triple quote and press Enter, it runs the whole thing.The line breaks inside the quotes are kept exactly as you typed them. Try it at the prompt below. Type the first line and press Enter — the prompt will change to ... and wait for you. Keep typing lines, and finish with ''') to close it.
'''It's a "lovely" day''' works fine. If you are ever unsure, triple quotes are the safest choice.6Recap
Which line runs without an error?
You want to print an address over three lines using ONE print(). What do you use?