One-sentence summary
The input() function lets a program ask the user a question while it is running and store the answer typed on the keyboard in a variable.
Why does it matter?
Until now, the programs we wrote always worked with the same values. We typed the numbers into the code ourselves, and the program gave the same result every time. But real programs are not like this: a calculator learns which numbers to add, a game learns the player's name, and a robot interface learns which command to run — all from the user.
Once we learn to get input from the user, our program starts talking with us. The same code can produce a different result every time it runs, depending on the answer it receives. This is a big step that makes a program genuinely useful.
Asking a question with input()
In Python we use the input() function to ask the user for information. We write a question or a short note inside the parentheses; this text appears on the screen and guides the user.
name = input("What is your name? ")
print("Hello " + name)
When the program runs, the text What is your name? appears and the program pauses to wait. When the user types something and presses Enter, what they typed is placed into the name variable. The next line then greets them.
Here input() does two things: first it shows the message in the parentheses, then it gives the user's answer back to us. If we do not store this answer in a variable, it is lost, so we always put it in a box like name = input(...).
input() always returns text
There is a very important rule here: whatever the user types, input() always gives the answer back as text (a string). Even if the user types 5, Python does not read it as the number 5; it reads it as the text "5".
Why does this matter? Because we cannot do number operations with text. Look at this code:
number = input("Enter a number: ")
print(number + 1) # ERROR
Even if the user types 5, this line does not run and produces an error. That is because number is actually the text "5", and Python does not know how to add the number 1 to a piece of text. Trying to add text and a number is like saying "apple + 1."
Converting text into a number
To turn the text the user typed into a number, we use the int() function. int comes from the word "integer," which means a whole number.
number = int(input("Enter a number: "))
print(number + 1)
We read this line from the inside out:
input("Enter a number: ")gets a piece of text from the user (for example"5").int(...)converts this text into a number (5).number = ...puts the result into thenumbervariable.
Now number is a real number, so we can do arithmetic with it. If the user types 5, the program prints 6.
Small note:int()is only used for whole numbers. If you need to read a decimal number (for example3.5) from the user, you use thefloat()function instead. In this lesson we will work with whole numbers.
Clean output with f-strings
When we print a result, we usually want to show text and a number together. The cleanest way to do this is with an f-string. We put the letter f in front of the text and write the variables inside curly braces {}.
name = input("What is your name? ")
age = int(input("How old are you? "))
print(f"Hello {name}, you are {age} years old.")
If the user enters Ada and 12, the screen shows:
Hello Ada, you are 12 years old.
Thanks to f-strings, we do not have to join text pieces with + or convert numbers into text; Python automatically places the value inside the curly braces where it belongs.
Example: Adding two numbers
Now let us combine what we have learned. We will write a program that takes two numbers from the user and shows their sum.
number1 = int(input("Enter the first number: "))
number2 = int(input("Enter the second number: "))
total = number1 + number2
print(f"{number1} + {number2} = {total}")
When the program runs, it asks two questions in order. If the user enters 7 and 8, we see:
7 + 8 = 15
Three ideas work together here:
input()gets the two values from the user.int()converts this text into real numbers, so addition can happen.- The f-string shows the result as a tidy sentence.
What would happen if we forgot to use int()? Then number1 + number2 would not be addition but joining two pieces of text side by side: 7 + 8 would give 78. This is the most common trap when working with input().
Mini practice
Write a program that asks the user's age and tells them how old they will be in 10 years.
- Use
int(input(...))to get the user's age and store it in anagevariable. - Create a new variable called
future_ageand give it the valueage + 10. - Use an f-string to print the result as a nice sentence. For example:
In 10 years you will be 22 years old.
After it works, try this: remove the int() function and run the program again. Observe which error you get. This helps you remember why input() returns text.
Common mistakes
Forgetting to use int()
This is the most common mistake. Because input() always gives text, if you do not convert it with int() before doing number operations, you will either get an error or an unexpected result (text stuck together).
number = input("Number: ") # number is text
number = int(input("Number: ")) # correct: number is really a number
Closing parentheses incorrectly
When you write int(input("...")) there are two opening parentheses, so there must also be two closing parentheses. If one is missing, Python gives a SyntaxError. Counting the code from the inside out helps.
When a letter is entered instead of a number
int() can only convert text that looks like a number. If the user types five instead of 5, the program gives an error. For now we assume the user enters the right kind of data; later we will learn how to handle these cases.
Not storing the answer in a variable
If you write input("What is your name? ") on its own, the answer is received but immediately lost. If you want to use the answer later, you must put it into a variable like name = input(...).
Safety note
When you write programs that take input from the user, there is a rule to make a habit: do not ask for or request personal or private information. Your practice programs should not ask for private things like an address, phone number, school name, password, or family details. To learn, harmless information such as a name, age, favourite colour, or a game score is enough.
Also, only run code that you wrote yourself or that you trust. Do not run a program you found online and do not understand without asking an adult first.
Lesson summary
input()lets us ask the user a question while the program runs and receive the answer.input()always returns the answer as text (a string), even if the user types a number.- To convert text into a number we use the
int()function:int(input(...)). - An f-string (
f"... {variable} ...") shows text and variables together cleanly. - We never ask the user for personal or private information such as an address or password.
Check questions
- What does the
input()function do, and what type does it return the answer as? - After
number = input("Number: "), why doesnumber + 1give an error? - Explain the line
int(input("Age: "))step by step, from the inside out. - If the user enters
4and5, what wouldprint(number1 + number2)print without usingint()? - Give two examples of information you should not ask the user for in a practice program.
Answers
input()shows a message on the screen while the program runs and receives the answer the user types and enters with the keyboard. It always returns this answer as text (a string).- Because
input()gives the answer as text;numberis actually a string like"4". Python cannot add a number to text, sonumber + 1gives an error. You first need to convert it withint(). - First
input("Age: ")gets a piece of text from the user. Thenint(...)converts this text into a whole number. The outer assignment then stores this number in a variable. - It prints
45. Withoutint(), both values are text; the+sign does not add two strings but joins them side by side. - For example: home address, phone number, password, school name, family details. (Two of these are enough.)
Source and verification note
For “Getting Input from the User”, verification focuses on whether the relationship between Asking a question with input() and Converting text into a number remains consistent across examples. Code examples follow Python 3 syntax. Small differences may appear between environments, so examples should first be tested in a safe online editor or a local development setup.
Next lesson
Conditions: With if, elif, and else we learn how a program can make different decisions based on the user's answer.