So far, we've been giving values directly to our Python programs. But real programs often need to interact with the user.
For example, a program might ask for your name, age, or favorite color. Python provides the input() function for this.
What is input()?
The input() function is used to take input from the user through the keyboard.
Let's see a simple example:
name = input("Enter your name: ")
print("Hello", name)If the user enters:
JohnThe output will be:
Hello JohnHere, whatever the user enters is stored in the name variable.
How Does input() Work?
Look at this line:
name = input("Enter your name: ")There are three things happening:
input()displays the message to the user.The program waits for the user to type something.
The entered value is stored in
name.
You can also use input() without a message:
name = input()But usually, providing a message makes the program easier to understand.
input() Always Returns a String
This is an important point to remember:
The
input()function always returns the user's input as a string.
For example:
age = input("Enter your age: ")
print(type(age))If the user enters 20, Python still treats it as:
<class 'str'>So this can cause a problem:
age = input("Enter your age: ")
print(age + 5)This will produce an error because Python cannot directly add an integer to a string.
Converting User Input
If you want to take a number, you need to convert the input into the required data type.
For an integer:
age = int(input("Enter your age: "))
print(age + 5)If the user enters 20, the output is:
25For a decimal number, use float():
price = float(input("Enter the price: "))
print(price)So, commonly you'll see:
int(input())for integers and:
float(input())for decimal numbers.
Taking Multiple Inputs
You can ask for multiple values separately:
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Name:", name)
print("Age:", age)Output might look like:
Enter your name: John
Enter your age: 20
Name: John
Age: 20A Practical Example
Let's create a simple program that calculates someone's age after five years:
name = input("Enter your name: ")
age = int(input("Enter your current age: "))
future_age = age + 5
print("Hello", name)
print("After 5 years, you will be", future_age)If the user enters:
Enter your name: John
Enter your current age: 20The program produces:
Hello John
After 5 years, you will be 25Taking Input in One Line
Python also allows us to take multiple inputs from a single line.
For example:
name, age = input("Enter name and age: ").split()
age = int(age)
print(name)
print(age)If the user enters:
John 20split() separates the input into different values.
For now, you don't need to worry too much about split(). We'll use it more when working with strings and collections.
Remember
The most important thing to remember is:
name = input("Enter your name: ")takes text input, while:
age = int(input("Enter your age: "))takes input and converts it into an integer.
With input(), your programs can finally start talking to the user and responding to their input, which makes them much more interactive.