Input
name = 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 withint()orfloat();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.
Try it yourselfExample 1: Ask a question
Edit the Input box and run again. Each line of input answers one input() call.
Output appears hereGreet by name
Fill the blanks so the program reads a name and prints Welcome, Rafi! when the input is Rafi.
name = () print(f"Welcome, {}!")
Example 2: Numbers from input
int() converts the typed text into a number so we can do maths with it.
Output appears hereExample 3: Two numbers on one line
split() breaks "12 30" into the pieces "12" and "30"; map(int, ...) converts both.
Output appears hereTwo 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
Rectangle from one line
Read the length and width on one line (like 5 3) and print Area: 15.
Example input5 3Expected outputArea: 15
Example 4: Formatting numbers
The part after the colon controls the format.
Output appears hereTwo decimal places
Fill the blank so the price prints as Price: 12.50.
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.
Output appears hereCelsius 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
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.
Example 6: A name and a mark on one line
Change the Input box to another student, such as Karim 91.
Output appears hereExample 7: Format specifiers
Each line shows one specifier. Try {price:10.2f} too.
Output appears here3Level 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.
Output appears hereExample 9: sep, end and repr
repr() reveals hidden spaces and newlines, which is great for debugging input. int() quietly ignores them.
Output appears hereage = input() followed by age + 1 raises TypeError: can only concatenate str (not "int") to str. Wrap it: age = int(input()).What does this print?
x = 3.14159
print(f"{x:.2f}")50 graded problems are waiting in Topic Practice.