Master Python

First steps

5 / 253
Contents

Contents

Master Python

0 of 253 complete

6 min read

Words and numbers on the same line

Printing several things at once, joining text end to end, and what happens when the two are mixed.

A sentence worth reading is usually a mixture: some words you wrote out yourself, and a number that came out of a sum. Everything printed so far has been one or the other, never both. There are two ways to get them onto the same line, and they behave differently enough to be worth taking one at a time.

Printing more than one thing

print will take several things at once if you separate them with commas, and it puts a single space between them as it writes them out. That is the simplest way to get some words and a number onto the same line.

Try CodePython

Change the digit 3 to 5 and run it again, then fix the word at the front so the sentence still tells the truth. Nothing checks that for you.

Joining two pieces of text

The + sign does something different to text. Applied to two numbers it adds them up. Applied to two pieces of text it joins them end to end, with nothing put in between: no space, no comma, nothing you did not write out yourself.

Try CodePython

Add a full stop as a third piece, + ".", and run it again. Then take the space out of "Hello, " and run it once more, to see that nothing is inserted for you.

What does print("3" + "4") write out?

34. Both values are text, because both are inside quotation marks, and + applied to two pieces of text joins them end to end rather than adding them up. Take the quotation marks away and print(3 + 4) writes 7.

Try CodePython

Take the quotation marks off the first line and run it again. Then put quotation marks on the second line instead.

Try CodePython

Change 3 to 20 and run it again. Then change 40 to 10 and see the line under it shrink.

Try CodePython

Run it and read the last line of the message. Then change the + to a comma and run it again. Then undo that and put quotation marks around the 30 instead, and compare the two outputs.

All lessons in Master Python