input()
Fetching user input with input()
So far, you have been using print() to send information to the user. But how do you get information from the user? The input() function asks user to type something into the command line that the program can then use.
# print() - sends information TO the user
print("Welcome to my app!")
# input() - gets information FROM the user
user_name = input("What's your name? ")
# Now you can use what they typed
print(f"Hello, {user_name}!")
Basic syntax
The basic pattern for input() is:
variable_name = input("Question or prompt for user? ")
Components:
input()function which gets user input- prompt string shown to the user to ask for the input (
"Question or prompt for the user? ") - variable (
variable_name) which stores the value entered by the user - assignment operator (
=)
E.g.
age = input("How old are you? ")
print(f"You are {age} years old")
What happens:
- The terminal displays
How old are you?How old are you? - Program waits for user to type the answer in the terminal and press Enter
How old are you? 3 - Whatever the user types is stored in the variable
age - The program uses the value of
agein the programHow old are you? 3 You are 3 years old
An important point to note is that you must save the input to a variable, otherwise it will be lost forever.
For example, with this code, the input entered by the user simply disappears
❌
input("What's your favorite color? ")
# User types a colour but it vanishes into thin air!
Instead you should need to save the value captured
✅
favorite_color = input("What's your favorite color? ")
print(f"Your favorite color is {favorite_color}")
# Value that the user types is saved in favorite_color
Formatting your prompts
The text you put inside input() is what the user sees. How you format it affects readability. You should think about using spaces and new lines to improve this.
If you simply use:
input("What's your favourite colour?") # No space
When the user types blue in response to the question, the terminal looks like this:
> What's your favourite colour?blue
If you want it to read more naturally, you’ll need to explicitly add a space or newline character (\n).
#Examples of formatting different formatting:
# No space:
input("What's your favourite colour?") # No space
# With space:
input("What's your favourite colour? ") # With space
# With new line:
input("What's your favourite colour?\n")
If the user entered blue, these would result in:
What's your favourite colour?Blue
What's your favourite colour? Blue
What's your favourite colour?
Blue
Handling input() return values
A critical point to note is that input() always returns a string, even if the user types a number or other data type
age = input("How old are you? ")
# User types: 25
print(type(age))
# Output: <class 'str'>
# This is a string "25", not the number 25!
This means you have to explicitly convert the input into an appropriate data type if you are going to use it as a data type.
For example, this would not work:
❌
age = input("How old are you? ")
next_year_age = age + 1 # Error! Can't add number to string
TypeError: can only concatenate str (not "int") to str
To use age as a number, you need to first convert it:
✅
age_str = input("How old are you? ")
age_num = int(age_str) # Convert string to integer
next_year_age = age_num + 1 # Now it works!
print(f"Next year you'll be {next_year_age}")
You can write this more succinctly as:
age = int(input("How old are you? "))
# Conversion happens immediately
Practice exercises
Solidfy your understanding of input() with these exercises.
Q1: Basic Input
Write a program that asks the user for their favorite food and then prints a message saying you like that food too.
Expected output:
What's your favorite food? Pizza
I love Pizza too!
See an example solution
favorite_food = input("What's your favorite food? ")
print(f"I love {favorite_food} too!")
Q2: Fix the Bug
This code has a bug. Can you spot and fix it?
input("What's your name? ")
print(f"Nice to meet you, {name}!")
See an example solution
name = input("What's your name? ")
print(f"Nice to meet you, {name}!")
Q3: Number Conversion
Write a program that:
- Asks the user how many apples they have
- Asks how many oranges they have
- Prints the total number of fruits
Expected output:
How many apples do you have? 5
How many oranges do you have? 3
You have 8 fruits in total!
Hint: Remember that input() returns a string. You’ll need to convert to integers before adding.
See an example solution
apples = int(input("How many apples do you have? "))
oranges = int(input("How many oranges do you have? "))
total = apples + oranges
print(f"You have {total} fruits in total!")
Or in a more compact form:
apples = int(input("How many apples do you have? "))
oranges = int(input("How many oranges do you have? "))
print(f"You have {apples + oranges} fruits in total!")
