Input & Output
Most useful programs talk to a person: they ask a question, wait for an answer and show a result. In Python, print() sends output to the screen and input() reads what the user types.
In this lesson you will control exactly how print() lays out its output, read input from the keyboard, turn that input into numbers, and stop your program from crashing when someone types something unexpected.
Printing output
You have already used print(). It can take several values separated by commas, and it puts a space between them and a new line at the end.
team = "Kochi Tuskers"
runs = 186
overs = 20
print(team, "scored", runs, "in", overs, "overs")
print("Run rate:", runs / overs)Kochi Tuskers scored 186 in 20 overs Run rate: 9.3
Notice that print() accepts numbers directly when you separate values with commas. You do not need str() here.
Changing the separator and the line ending
print() has two useful options, called keyword arguments because you pass them by name:
sepsets what goes between the values. The default is one space.endsets what goes after the last value. The default is a new line, written"\n".
print("12", "08", "2026", sep="-")
print("Loading", end="")
print("...", end=" ")
print("done")
print("Aarav", "Meera", "Kabir", sep=", ")12-08-2026 Loading... done Aarav, Meera, Kabir
Because the second and third print() calls change end, they do not move to a new line, so "Loading", "..." and "done" end up on the same line. You will learn more ways to pass arguments in Function Arguments.
Reading input with input()
input() shows a message, called a prompt, then waits until the user types something and presses Enter. It gives back what they typed as a string.
name = input("What is your name? ")
city = input("Which city are you from? ")
print(f"Hello {name}, greetings to everyone in {city}!")When you run this on this site, the browser opens a small box for each question. In a terminal, it looks like this (the text after each ? is what the user typed):
What is your name? Ananya
Which city are you from? Kochi
Hello Ananya, greetings to everyone in Kochi!End the prompt with a space, as in "What is your name? ", otherwise the answer is typed right against the question mark.
input() always returns a string
This is the most important fact in this lesson. Even if the user types 21, input() gives you the string "21", not the number 21. To do maths, convert it with int() for whole numbers or float() for decimals.
age = int(input("Your age: "))
height = float(input("Your height in metres: "))
print("In five years you will be", age + 5)
print("Your height in cm is", height * 100)Your age: 21
Your height in metres: 1.68
In five years you will be 26
Your height in cm is 168.0Wrapping input() inside int() converts the answer straight away. Without int(), the line age + 5 would fail with TypeError: can only concatenate str (not "int") to str, because Python cannot add a number to text.
Several values on one line
Sometimes it is easier to type several values at once, such as 85 90 78. The string method split() breaks text at the spaces and gives you a list of pieces:
answer = input("Enter marks in three subjects: ")
parts = answer.split()
maths = int(parts[0])
science = int(parts[1])
english = int(parts[2])
total = maths + science + english
print("Total:", total, "out of 300")Enter marks in three subjects: 85 90 78
Total: 253 out of 300parts is a list such as ["85", "90", "78"]. Each piece is still a string, so each one goes through int(). You will learn neater ways to convert a whole list in Comprehensions and more about split() in String Methods.
Handling bad input
People make typing mistakes. If someone types twenty when you call int(), the program stops with ValueError: invalid literal for int() with base 10: 'twenty'. You can check the text first: the string method isdigit() is True only when every character is a digit.
text = input("How many tickets? ").strip()
if text.isdigit():
tickets = int(text)
print("Amount to pay: ₹", tickets * 150)
else:
print("Please type a whole number, like 2.")How many tickets? two
Please type a whole number, like 2.strip() removes spaces the user may have typed by accident. The if and else choose what to do; they are explained in if, elif & else. The professional way to handle bad input is try and except, which you will learn in Errors & Exceptions.
Common mistakes
- Doing maths on input directly.
input("Age: ") + 1fails. Convert first:int(input("Age: ")) + 1. - Using
int()for decimal input.int("4.5")fails. Usefloat()when the user may type a decimal point. - Forgetting the space in the prompt.
input("Name:")makes the answer touch the colon. Add a space at the end. - Joining numbers with
+inprint().print("Total: " + total)fails whentotalis a number. Use a comma, or an f-string:print(f"Total: {total}").
Key points
print()shows values separated by a space and ends with a new line.sepchanges the separator andendchanges the line ending.input("prompt ")shows a prompt and returns what the user typed.input()always returns a string; useint()orfloat()to get a number.split()breaks a line of input into a list of strings.- Check input with methods like
isdigit()before converting; later you will usetryandexcept.
Exercise
Write a program for a chai stall. Ask the customer's name and how many cups they want. Each cup costs ₹15. Print a bill in this format: Bill for Rohan: 3 cups x ₹15 = ₹45. If the number of cups is not a whole number, print a polite message instead.
Show solution
CUP_PRICE = 15
name = input("Your name: ").strip()
cups_text = input("How many cups? ").strip()
if cups_text.isdigit():
cups = int(cups_text)
print(f"Bill for {name}: {cups} cups x ₹{CUP_PRICE} = ₹{cups * CUP_PRICE}")
else:
print("Sorry, please enter the number of cups as digits.")The cup count is checked with isdigit() before int() converts it, so a typing mistake prints a message instead of crashing. The f-string builds the bill line from the variables.
Quick Quiz
1. What type of value does input() return?
input() always returns a string; you must convert it yourself.
2. What does print("A", "B", "C", sep="-") print?
sep sets the text placed between the values.
3. Which line correctly reads a whole number from the user?
input() runs first and returns text, and int() converts that text to a number.
4. What does "20 35 50".split() give?
split() breaks a string at whitespace and returns a list of strings, not numbers.
Learn Python with a mentor
Free lessons get you started. Our Python Programming training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
