Input & Output
Programs become useful when they talk to people. Output means showing information, usually with print(). Input means reading what the user types, with input(). Together they let you build interactive programs such as calculators, quizzes and menus.
Output with print()
You've already used print(). It can take any number of values, separated by commas, and prints them on one line with spaces between them. Each call then moves to a new line.
name = "Faith"
age = 22
print("Name:", name, "| Age:", age)
print(f"{name} will be {age + 1} next year.")
Changing the separator with sep
The optional sep argument sets what goes between the values (a single space by default).
print("2026", "11", "15", sep="-")
print("a", "b", "c", sep="")
print("Nairobi", "Kisumu", "Mombasa", sep=" → ")
Changing the ending with end
The optional end argument sets what is printed after the last value. By default it is "\n", a new line. Change it to keep the next output on the same line.
print("Loading", end="...")
print("done")
print("No", end=" ")
print("new line here")
Printing several lines at once
print("""MENU
1. Check balance
2. Buy airtime
3. Exit""")
Input with input()
input() pauses the program, shows an optional message called a prompt, waits for the user to type something and press Enter, and then gives back what they typed.
In the sample run below, the user types Achieng:
name = input("What is your name? ")
print(f"Habari, {name}! Karibu.")
Notice the space at the end of the prompt "What is your name? ". Without it, the user's typing would be squashed against the question mark.
In the sample runs on this site, text that the user types is shown in the output as it would appear on screen, straight after the prompt. Your output depends on what you type.
input() always returns a string
This is the single most important fact about input(). Even if the user types digits, you get text. To do maths, convert it first with int() or float().
age = input("How old are you? ")
print(type(age))
Doing maths directly on input. If age = input(...) and you write age + 1, Python stops with TypeError: can only concatenate str (not "int") to str. Convert the input first: age = int(input("How old are you? ")).
age = int(input("How old are you? "))
print(f"In 5 years you will be {age + 5}.")
Converting between types is covered fully on the next page, Type Conversion, including what happens when the user types something that isn't a number.
A complete interactive program
This program asks for an item and its price, then prints a receipt with 16% VAT added. The output shows a sample run.
item = input("Item: ").strip()
price = float(input("Price in KES: "))
vat = price * 0.16
total = price + vat
print("----- RECEIPT -----")
print(f"{item.title()}: KES {price:,.2f}")
print(f"VAT (16%): KES {vat:,.2f}")
print(f"Total: KES {total:,.2f}")
Calling .strip() on the input removes accidental spaces before or after what the user typed, a good habit whenever you read text.
Forgetting the brackets when you store input. name = input (no brackets) doesn't ask the user anything; it just makes name another name for the input function itself. Always call it: input().
Key points
print()shows values;sepcontrols what goes between them andendwhat comes after.input(prompt)shows a prompt, waits for the user and returns what they typed.input()always returns a string; useint()orfloat()before doing maths.- Use
.strip()to tidy up text input, and f-strings to format the results.
Tip: use the ← and → keys to move between pages.
