LambdaLabTM
Computer Science · Class 11 · Displaying Output
OutputStrings⏱️ 8 min read

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.

Python prompt — interactive mode
>>> print('Ramesh')
Ramesh
>>> print('Class 11 · Section B')
Class 11 · Section B
>>> print('42')
42

That last one is worth a second look. It printed 42, but because of the quotes it is text, not a number.

Watch Out
That last line is important. '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.

1 · Pick a message to print

An apostrophe — the most common mistake.

2 · Wrap it in a quote
print('It's a beautiful day')
✗ SyntaxError
the string ended in the wrong place
Why it broke

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

' … '
Single quotes

Use these most of the time. They work for normal text.

'He left at noon'
" … "
Double quotes

Use these when your text has an apostrophe in it, like It's or don't.

"It's here"
''' … '''
Triple quotes

Use these when your text goes over more than one line. They also allow both other quote marks inside.

'''Two\nlines'''
Key Takeaway
Single and double quotes do the same job. Normally you can pick either one. The choice only matters when your text contains a quote mark. Text with an apostrophe? Use double quotes. Text with " 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:

Python prompt — interactive mode
>>> print('It's a beautiful day')
File "<stdin>", line 1
print('It's a beautiful day')
^
SyntaxError: unterminated string literal (detected at line 1)

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:

Python prompt — interactive mode
>>> print("It's a beautiful day")
It's a beautiful day
>>> print('He said "Hello" and left')
He said "Hello" and left

Try both at the prompt below. Then swap the quotes around — put single quotes around It's — and watch Python break.

Python prompt — interactive mode
# Type a print() line. Then try it with the wrong quotes.
>>>
try

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 """.

Python prompt — interactive mode
>>> print('''Roses are red,
... Violets are blue,
... Python is lovely,
... And so are you.''')
Roses are red,
Violets are blue,
Python is lovely,
And so are you.
Note
What are those ... 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.

Python prompt — interactive mode
# Type the first line, press Enter, and watch the prompt change to ...
>>>
try
Tip
Triple quotes accept everything inside them, including apostrophes and double quotes. So '''It's a "lovely" day''' works fine. If you are ever unsure, triple quotes are the safest choice.

6Recap

Key Takeaway
A string is text inside quotes. Use single quotes normally. Use double quotes when the text has an apostrophe. Use triple quotes when the text runs over several lines. The quotes are only a wrapper — they are never printed.
Quick Check

Which line runs without an error?

Quick Check

You want to print an address over three lines using ONE print(). What do you use?