LearnPy Hub
BasicsBeginnerLesson 5 of 32

Input

Watch the ideaTalking to the user
name = input("Name? ")
⌨ Mim
input()
name
Name? 

input() shows the prompt and waits for the user to type.

1Level 1 · BasicsBasics

So far our programs have always done the same thing. With input, a program can ask the user a question and react to the answer. This lesson also covers neat output with f-strings, and comments, which are notes for humans.

Reading input with input()

input() pauses the program, waits for the user to type a line, and gives you that line as a string:

name = input("What is your name? ")
print("Hello,", name)

The text inside the brackets is the prompt shown to the user. In the examples below there's an Input box under the code: each line in it answers one input() call.

Input is always text

Even if the user types 25, input() returns the string "25". Convert it before doing maths:

age = int(input("Age: "))       # whole number
price = float(input("Price: "))  # decimal number

Several values on one line

Practice problems often give several numbers on one line, like 3 8. Split the line on spaces, then convert each part:

a, b = map(int, input().split())

split() breaks the text into pieces, and map(int, ...) converts each piece to an int.

Nicer output with f-strings

Put an f before the quotes and write variables inside curly braces:

print(f"{name} is {age} years old")
print(f"Total: {price * 3:.2f}")   # :.2f shows 2 decimal places

Inside the braces you can put any expression, and after a colon a format, such as :.2f for two decimals, :, for thousands separators, or :>8 to right-align in 8 characters.

Comments

Anything after # on a line is a comment. Python ignores it. Comments explain why the code does something, and they let you switch a line off while experimenting.

# Convert Celsius to Fahrenheit
f = c * 9 / 5 + 32   # formula from physics class
Key points: input() returns a string, so convert with int() or float(); input().split() handles several values on a line; f-strings make readable output; # starts a comment.

See it in actionInput arrives as text

Each input() takes the next line of input. int() turns the text into a number.

Step-by-step

Try it yourselfExample 1: Ask a question

Edit the Input box and run again. Each line of input answers one input() call.

example_1.py
Output appears here
Fill in the blanks

Greet by name

Fill the blanks so the program reads a name and prints Welcome, Rafi! when the input is Rafi.

blanks.py
name = ()
print(f"Welcome, {}!")

Example 2: Numbers from input

int() converts the typed text into a number so we can do maths with it.

example_2.py
Output appears here

Example 3: Two numbers on one line

split() breaks "12 30" into the pieces "12" and "30"; map(int, ...) converts both.

example_3.py
Output appears here
Fix the bug

Two numbers on one line

The input is two numbers on one line, like 3 4. The program should print their product 12, but it crashes. Fix it.

Example input3 4Expected output12

buggy.py
Your turn

Rectangle from one line

Read the length and width on one line (like 5 3) and print Area: 15.

Example input5 3Expected outputArea: 15

my_code.py

Example 4: Formatting numbers

The part after the colon controls the format.

example_4.py
Output appears here
Fill in the blanks

Two decimal places

Fill the blank so the price prints as Price: 12.50.

blanks.py
price = 12.5
print(f"Price: {price:}")

Example 5: Comments

Python skips everything after #. Remove the # on the last line to switch it back on.

example_5.py
Output appears here
Your turn

Celsius to Fahrenheit

Read a temperature in Celsius (a whole number) and print it in Fahrenheit with one decimal place, like 98.6 F for 37. Use F = C × 9 / 5 + 32.

Example input37Expected output98.6 F

my_code.py

2Level 2 · IntermediateReading many values and formatting numbers

Contest problems and real programs often read several values at once and print them in a fixed format.

map() for several numbers

a, b, c = map(int, input().split()) reads three whole numbers from one line. The number of names on the left must match the number of values on the line.

Mixed values on one line

When a line holds text and a number, like Rahim 78, split it into names first and convert only the number: name, mark = input().split(), then mark = int(mark).

Format specifiers

In an f-string, after a colon: {x:.2f} two decimals, {n:,} thousands separators, {p:.1%} percentage, {n:05d} pad with zeros.

See it in actionSplitting a line into numbers

Watch the three pieces of text become three numbers, then their total.

Step-by-step

Example 6: A name and a mark on one line

Change the Input box to another student, such as Karim 91.

example_6.py
Output appears here

Example 7: Format specifiers

Each line shows one specifier. Try {price:10.2f} too.

example_7.py
Output appears here

3Level 3 · AdvancedExact output with alignment, sep, end and repr

Practice problems check your output character by character, so it pays to control every space.

Aligning columns with f-strings

Inside {} you can set a width and alignment: {name:<10} pads to 10 characters on the left, {price:>6} on the right, {x:^9} centres. This is how real programs print neat tables.

print() options

sep sets what goes between the values and end what goes after them. print(1, 2, 3, sep=", ") prints 1, 2, 3.

repr() shows hidden characters

repr() shows a value the way Python sees it, including quotes, spaces and \n. It is the quickest way to find out why input "looks right" but doesn't match. int() and float() ignore spaces around a number.

Example 8: A neat receipt with aligned columns

Try changing the widths (12, 4, 8) and see how the table moves.

example_8.py
Output appears here

Example 9: sep, end and repr

repr() reveals hidden spaces and newlines, which is great for debugging input. int() quietly ignores them.

example_9.py
Output appears here
Common mistakeage = input() followed by age + 1 raises TypeError: can only concatenate str (not "int") to str. Wrap it: age = int(input()).
Exercise · predict the output

What does this print?

x = 3.14159
print(f"{x:.2f}")
Quick quiz · 5 questions
1. If the user types 10, what does input() return?
2. How do you read two integers from one line like "4 9"?
3. Which symbol starts a comment in Python?
4. What does f"{2 + 3}" produce?
5. What does the :.1f in f"{x:.1f}" do?
Ready to practice input?

50 graded problems are waiting in Topic Practice.

Practice this topic →
Python Input · LearnPy Hub