Python User Input
Introduction to User Input in Python
User input is an important concept in Python programming. It allows users to enter information while a program is running.
Python provides a built-in input() function to accept data from users through the keyboard.
For example, a program can ask users to enter their name, email address, age, or numbers and then process that information.
In this tutorial, you will learn how to use the input() function, store user input in variables, convert input into numeric values, and perform calculations using user-provided data.
1. What Is User Input in Python?
User input refers to the information entered by a user while a program is executing.
Python uses the input() function to receive information from users.
Syntax
input("Enter your value: ")
The text inside the input() function is displayed as a prompt asking the user to enter information.
Example
input("Enter Email: ")
Output:
Enter Email:
After displaying the prompt, Python waits for the user to enter a value.
For example, if the user enters:
student@gmail.com
Python receives the entered value as input.
2. The input() Function in Python
The input() function is a built-in Python function used to accept information from users.
By default, the input() function returns the entered value as a string (str), even when the user enters a number.
Example 1: Taking User Input
name = input("Enter your name: ")
print("Welcome", name)
Example Input:
Enter your name: Rahul
Output:
Welcome Rahul
In this example:
input()displays a message asking for the user’s name.- The entered value is stored in the
namevariable. print()displays the entered name.
Example 2: Taking Email Input
email = input("Enter Email: ")
print("Your Email:", email)
Example Input:
Enter Email: student@gmail.com
Output:
Your Email: student@gmail.com
3. Understanding the Return Type of input()
One important thing to remember is that the input() function always returns a string.
Even if a user enters a numeric value, Python treats it as text until it is converted into another data type.
Example
age = input("Enter your age: ")
print(age)
print(type(age))
Example Input:
Enter your age: 22
Output:
22
<class 'str'>
Although the user entered 22, Python stores it as a string.
This is important because mathematical calculations require numeric values rather than strings.
4. Taking Integer Input in Python
To accept an integer value from the user, you can combine the input() function with the int() function.
The int() function converts a valid integer string into an integer.
Syntax
variable = int(input("Enter a number: "))
Example
age = int(input("Enter your age: "))
print(age)
print(type(age))
Example Input:
Enter your age: 22
Output:
22
<class 'int'>
In this example, int() converts the input string into an integer.
5. Taking Float Input in Python
If you want to accept decimal numbers from users, you can use the float() function.
Example
price = float(input("Enter product price: "))
print("Product Price:", price)
print(type(price))
Example Input:
Enter product price: 99.50
Output:
Product Price: 99.5
<class 'float'>
Here, the float() function converts the entered value into a floating-point number.
6. Performing Calculations Using User Input
You can accept multiple values from users and perform mathematical calculations using those values.
Example: Addition of Two Numbers
# Take input from users and store them in variables
fnum = int(input("Enter first number: "))
snum = int(input("Enter second number: "))
# Add the two variables
result = fnum + snum
# Print the result
print(result)
# Check the data type
print(type(fnum))
Example Input:
Enter first number: 10
Enter second number: 20
Output:
30
<class 'int'>
Explanation
input()accepts the first number from the user.int()converts the entered value into an integer.- The first value is stored in
fnum. - The second value is stored in
snum. - The
+operator adds both numbers. - The
print()function displays the result. type()checks the data type offnum.
7. Common Mistakes When Taking User Input
Beginners often make mistakes when working with user input.
Mistake 1: Adding Two Numbers Without Type Conversion
Incorrect:
a = input("Enter first number: ")
b = input("Enter second number: ")
print(a + b)
Example Input:
10
20
Output:
1020
Why does this happen?
Because input() returns strings, Python joins the two strings instead of performing numerical addition.
Correct:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print(a + b)
Output:
30
Mistake 2: Entering Invalid Numeric Values
age = int(input("Enter your age: "))
If the user enters text such as hello instead of a valid integer, Python raises a ValueError.
You should ensure that users enter values in the expected format or use exception handling to handle invalid input.
Mistake 3: Forgetting Parentheses
Incorrect:
name = input
Correct:
name = input("Enter your name: ")
The function must be called using parentheses to receive input.
8. Practice Questions on Python User Input
Test your understanding with these multiple-choice questions.
Question 1: Which built-in function is used to accept input from users in Python?
A. print()
B. input()
C. type()
D. display()
Answer: B. input()
Question 2: What is the default return data type of the input() function?
A. Integer
B. Float
C. String
D. Boolean
Answer: C. String
Question 3: Which function converts a valid numeric string into an integer?
A. str()
B. float()
C. int()
D. type()
Answer: C. int()
Question 4: What will be the output?
a = input("Enter first number: ")
b = input("Enter second number: ")
print(a + b)
If the user enters 10 and 20, what will be displayed?
A. 30
B. 1020
C. 10 + 20
D. Error
Answer: B. 1020
Question 5: Which function is used to accept decimal input?
A. int()
B. str()
C. float()
D. bool()
Answer: C. float()
Question 6: What will be the output?
a = int(input("Enter a number: "))
print(type(a))
If the user enters 100, what will be displayed?
A. <class 'str'>
B. <class 'float'>
C. <class 'int'>
D. <class 'bool'>
Answer: C. <class 'int'>