livepython.net
CHAPTER 02The Print Function
2.1  PRINT() BASICS

Python print() Function: Show Text and Numbers

What Is a Function?

In chapter 1 you wrote print("Hello, World!"). That print is a function. Before we learn all its tricks, let's see what a function is.

A function is a named mini-machine that does one job. You don't need to know how it works inside. You just call it by its name, give it what it needs, and it does the job.

Think of a juice machine. You put fruit in, press the button, and juice comes out. You don't build the machine every time. You just use it.

How to call a function

You call (use) a function by writing its name, then brackets:

print("Hi")
  • print is the function's name.
  • The brackets ( ) say "run it now".
  • What goes inside the brackets is what you give the function. Here, the text "Hi".

print() is a built-in function: it comes with Python, ready to use. Python has many more, and later you'll even write your own.

Why functions are great

  • Reuse: you write print() once in a line, and it works every time.
  • Less to think about: you don't need to know how Python puts letters on the screen.
  • Clean code: one short name does a whole job.

A function is a named mini-machine: call it with its name and brackets.

your turnPick the right idea

Which line calls a function?

Think about it, then pick one.

Printing Text (Strings)

To show text with print(), put the text inside quotes:

PYTHONedit me
print("Good morning!")
OUTPUT
Good morning!

Text in code has a special name: a string. You'll hear this word a lot. A string is any text inside quotes: a word, a sentence, even a single letter.

Single or double quotes?

Python accepts both. These two lines do exactly the same thing:

PYTHONedit me
print("I love Python")
print('I love Python')
OUTPUT
I love Python
I love Python

Just start and end with the same kind of quote. Many people use double quotes, but choose one and stay with it.

Why the quotes matter

Quotes tell Python: "this is text, don't try to understand it". Without quotes, Python thinks the word is the name of something, and looks for it:

PYTHONedit me
print(Hello)
OUTPUT
NameError: name 'Hello' is not defined

Python looked for something called Hello, didn't find it, and stopped with a NameError. Add the quotes, and it works.

Common beginner doubt: "Are the quotes printed?" No! They only mark where the text starts and ends.

Text goes inside quotes. Python calls it a string.

your turnPredict the output

What will this print?

print('Hi there')
Your turn! What will this code show on the screen?
your turnSpot the bug

Which line breaks?

One line stops this program. Tap it.

Printing Numbers

Numbers don't need quotes. Python understands them on its own:

PYTHONedit me
print(25)
print(3.5)
print(-10)
OUTPUT
25
3.5
-10

Whole numbers, decimal numbers and negative numbers all work.

"25" or 25?

Here's something interesting. These two lines show exactly the same thing:

PYTHONedit me
print("25")
print(25)
OUTPUT
25
25

They look the same on the screen, but they are different inside Python:

You writePython seesIt can do
"25"text (a string) that happens to contain digitstext things
25a real numbermaths

This difference matters a lot when you start calculating. For now, remember the rule:

  • Text needs quotes.
  • Numbers don't.

When to use which

Use a number when you might want to do maths with it: an age, a price, a score. Use text for everything else: names, messages, even a phone number, since you never add phone numbers together.

Numbers without quotes, text with quotes.

your turnPick the right idea

Which of these must be in quotes?

Think about it, then pick one.

Printing Several Items at Once

You can give print() several things at once. Put a comma between them:

PYTHONedit me
print("Sunny", "Rocky", "Lucky")
OUTPUT
Sunny Rocky Lucky

print() shows all of them on one line, with one space between each.

Mixing text and numbers

Commas also let you mix text and numbers in one line. This is very useful:

PYTHONedit me
print("I am", 25, "years old")
print("Score:", 90)
OUTPUT
I am 25 years old
Score: 90

Notice that you didn't type the spaces between the items. print() added them for you. That's why "I am" has no space at its end, yet the output reads I am 25.

Commas inside quotes are just text

Be careful: a comma inside the quotes is part of the text. A comma outside the quotes separates items.

PYTHONedit me
print("Hello, Sunny")
print("Hello", "Sunny")
OUTPUT
Hello, Sunny
Hello Sunny

The first line prints one string that contains a comma. The second prints two strings with a space between them.

Commas outside quotes separate items, and print() puts a space between them.

your turnPredict the output

What will this print?

print("Age:", 25)
Your turn! What will this code show on the screen?
your turnFill the gap

Fill the gap

print("Sunny" ? "Rocky")

It should print: Sunny Rocky

Which piece makes this code work?

Doing Math Inside print()

Python is a great calculator. Put a calculation inside print(), and Python shows the answer:

PYTHONedit me
print(5 + 3)
print(10 - 4)
print(6 * 7)
print(20 / 4)
OUTPUT
8
6
42
5.0
SymbolMeaningExampleResult
+add5 + 38
-subtract10 - 46
*multiply6 * 742
/divide20 / 45.0

Python calculates first, then prints the result. Notice that / always gives a decimal like 5.0. You'll learn why in the Data Types chapter.

Quotes change everything

Put the same calculation in quotes, and it becomes plain text:

PYTHONedit me
print("5 + 3")
print(5 + 3)
OUTPUT
5 + 3
8

In quotes, Python doesn't calculate. It just shows the characters.

Text and an answer together

Use a comma to show a message next to the answer:

PYTHONedit me
print("5 + 3 =", 5 + 3)
OUTPUT
5 + 3 = 8

The first item is text, so it's shown as it is. The second is a calculation, so Python works it out first.

No quotes: Python calculates. In quotes: Python just shows the text.

your turnPredict the output

What will this print?

print("2 + 2")
Your turn! What will this code show on the screen?
your turnType the output

Type what this prints

print(10 - 4)
Read the code line by line, then type the output exactly.
your turnOrder the lines

Put the lines in order to print a countdown

  1. print("Go!")
  2. print(2)
  3. print(1)
  4. print(3)
These lines are mixed up. Put them back in order, then press Check.

Practice: print() basics

Solve these to lock in what you learned in this topic. Each one runs in your browser.

  1. 2.Python Program to Print a Phone Number Without QuotesEasySolve
  2. 3.Python Program to Print a String and a Number TogetherEasySolve
  3. 4.Python Program to Print a Message and a Calculation in One LineEasySolve
  4. 13.Python Program to Print Multiple LinesEasySolve