Taking input from the user is a fundamental aspect of programming, and Python provides flexible ways to do this. Whether you need to capture a number, a string, or any other type of data, Python makes it straightforward. Below, we’ll explore the different methods to take input from users, along with examples and best practices for Python 3, as Python 2 is now outdated.
In Python, there is one primary function for user input:
input()
input()
always returns the user’s input as a string.Here is how the input()
function works in Python 3:
# Example of taking string input
a = input("Enter your favorite website: ")
print(a)
print(type(a))
Output:
Enter your favorite website: FACE Prep
FACE Prep
<class 'str'>
Even if you input a number, it will still be treated as a string:
# Example of input with a number
a = input("Enter a number: ")
print(a)
print(type(a))
Output:
Enter a number: 7
7
<class 'str'>
To use this number in a mathematical operation, you need to convert it:
# Converting string input to integer
a = int(input("Enter a number: "))
print(a + 10)
Output:
Enter a number: 7
17
Since input()
returns a string in Python 3, typecasting is essential to use the inputted data as other types (e.g., integers or floats). Below are examples for typecasting:
a = int(input("Enter an integer: "))
print(a)
print(type(a))
a = float(input("Enter a decimal number: "))
print(a)
print(type(a))
You can also convert input to other types like lists or tuples if needed:
a = list(input("Enter a list of numbers separated by spaces: ").split())
print(a)
print(type(a))
When converting input to a specific data type, there’s a chance of encountering errors if the input is not compatible. To avoid crashes, use try
–except
blocks:
try:
a = int(input("Enter an integer: "))
print("The number you entered is:", a)
except ValueError:
print("Invalid input! Please enter a valid integer.")
input()
different from raw_input()
?raw_input()
always returns a string.input()
evaluates the input (e.g., entering 7
returns an integer).raw_input()
is removed, and input()
behaves like raw_input()
from Python 2 (always returning a string).You can take multiple inputs using the split()
method:
# Taking multiple inputs
inputs = input("Enter multiple values separated by spaces: ").split()
print(inputs)
To convert these inputs to integers or floats:
numbers = list(map(int, input("Enter multiple integers separated by spaces: ").split()))
print(numbers)
Always include clear prompts, and use typecasting to ensure the data is in the correct format. Wrap the input process in a try
–except
block to handle errors gracefully.
Taking user input in Python is straightforward using the input()
function, which reads input as a string by default. To work with different data types, the input needs to be converted explicitly using functions like int()
for integers and float()
for decimal values.
By mastering user input in Python, you can create more dynamic and interactive programs. Click here to know more our program!