EN · USD

Language

Prices in

USDUSD · your currency EuroEUR · what you pay

You always pay in euros. Other currencies are a guide at today's rate.

Become a tutor Sign up
Try:
Search tips: combine words
python list
Articles with all the words
"for loop"
The exact words, in this order
python OR javascript
Either word
functions -arrow
Leave out articles with a word
title:booleans
Only in the title
code:append
Only in code examples
subject:python
In one subject
by:maximilian
By one writer
series:javascript
In one series
level:beginner
At one level
Try:
Search tips: combine words
python list
Articles with all the words
"for loop"
The exact words, in this order
python OR javascript
Either word
functions -arrow
Leave out articles with a word
title:booleans
Only in the title
code:append
Only in code examples
subject:python
In one subject
by:maximilian
By one writer
series:javascript
In one series
level:beginner
At one level

Python Beginner English 6 min read

print(), variables, f-strings and input(): your first program that talks back

Show values with print(), keep and change them in variables, build sentences with f-strings, and ask the user questions with input(). Plus the mistake every beginner makes with input(), and a small receipt program to write yourself.

Series · Lesson 2 of 3 Python, one lesson a day
In this article9
  1. Your first line: print()
  2. Variables: a name for a value
  3. A quick reminder: strings and numbers
  4. f-strings: values inside a sentence
  5. input(): ask the user something
  6. input() always gives you a string
  7. Common mistakes
  8. Exercise
  9. Quick check

In lesson 1 you met Python's data types. Now you'll put them to work: show values with print(), keep them in variables and change them, build sentences with f-strings, and ask the user a question with input(). By the end you'll write a small program that talks to its user. Every example runs right here: press Run, then press Edit and change it.

Your first line: print()

print() shows whatever you put in its brackets. Text goes in quotes:

print("Hello, world!")
print("I'm learning Python.")
print(7 + 3)

Three things to notice:

  • Text in quotes is a string. You can use "double" or 'single' quotes, as long as both ends match.
  • Numbers have no quotes. 7 + 3 is maths, so Python prints 10.
  • Each print() starts a new line.

You can give print() several values, separated by commas. It puts a space between them:

print("Apples:", 4, "Pears:", 2)

Variables: a name for a value

A variable keeps a value under a name, so you can use it later. You make one with =:

name = "Ada"
age = 36

print(name)
print(age)
print(name, "is", age, "years old")

Unlike JavaScript, there's no let, const or var. You just write the name, =, and the value. You can give a variable a new value whenever you like:

score = 0
print(score)

score = score + 10
print(score)

score += 5   # the short way to write: score = score + 5
print(score)

Everything after # is a comment: a note for people, which Python skips.

A quick reminder: strings and numbers

From lesson 1: "7" in quotes is a str, 7 without quotes is an int. That matters as soon as you use +:

print(7 + 7)       # 14: adding numbers
print("7" + "7")   # 77: joining two strings
print("ha" * 3)    # hahaha: a string repeated

Keep this in mind: it's the reason for the most common input() mistake further down.

f-strings: values inside a sentence

Joining text with + gets messy fast. Python has a better way, the f-string: put f before the quotes, and write variables in {curly brackets}:

name = "Ada"
age = 36

print(f"My name is {name} and I am {age} years old.")
print(f"Next year I'll be {age + 1}.")

Anything inside {} is real Python, so you can even do sums there, like {age + 1}. This is the way most Python code builds text today.

input(): ask the user something

input() stops, waits for the user to type an answer, and gives it back to you. The text in the brackets is the question.

Here the answers come from the Input box under the code: one line for each input(). Type your name there, then press Run:

name = input("What's your name? ")
print(f"Nice to meet you, {name}!")

input() always gives you a string

This is the most common surprise for beginners. Even if the user types 12, input() gives you the text "12", not the number. To do maths with it, turn it into a number with int() (whole numbers) or float() (decimals).

Put two lines in the Input box, for example Ada and 36, and run it:

name = input("Name: ")
age = int(input("Age: "))

print(f"{name}, in ten years you'll be {age + 10}.")

int(input("Age: ")) reads from the inside out: first input() gets the text, then int() turns it into a number.

Common mistakes

Exercise

Show the answer
product = input("Product: ")
price = float(input("Price: "))
amount = int(input("How many: "))

total = price * amount
print(f"{amount} x {product} = {total} euros")

The price uses float() because it can have decimals, the amount uses int() because you can't buy half a coffee. Storing the sum in total keeps the f-string short and easy to read.

Want exactly two decimals, like 7.50? Write {total:.2f} inside the f-string.

Quick check

Quick check

What does this print?

a = "5"
b = "5"
print(a + b)
Why

a and b are strings, because of the quotes. + joins two strings, so you get "55". To get 10, write the numbers without quotes, or turn them into numbers with int().

Quick check

The user types 20. What is the type of age here: age = input("Age: ")?

Why

input() always gives a string, whatever the user types. Use int(input(...)) to get a number.

More from Maximilian Bahrami