So far, our programs have worked with data that we've directly written into the code. However, many applications need to interact with the user, asking for information during runtime. Python provides a straightforward way to achieve this using the built-in input()
function.
When your program calls input()
, it pauses execution and waits for the user to type something into the console and press the Enter key. Whatever the user types is then returned by the function as a string.
Let's try a basic example. Run the following code:
print("What is your name?")
user_name = input()
print("Hello,", user_name)
When you run this, the program will print "What is your name?", then wait. If you type Alice
and press Enter, the program will resume and print Hello, Alice
. The text you entered, Alice
, was captured by input()
and stored in the user_name
variable.
It's generally more user-friendly to include a prompt message directly within the input()
function. This message is displayed to the user, indicating what information they should enter.
user_name = input("Enter your name: ")
print("Hello,", user_name)
This version achieves the same result as the previous one but combines the prompt and the input request into a single line, making the code slightly more concise. The string "Enter your name: "
is displayed, and the program waits for input immediately after it.
A very important aspect of the input()
function is that it always returns the data entered by the user as a string, regardless of what they type. Even if the user enters digits, input()
treats them as characters forming a string.
Consider this example:
age_input = input("Enter your age: ")
print("Type of age_input:", type(age_input))
# Attempting addition might cause an error or unexpected behavior
# next_year = age_input + 1 # This would raise a TypeError
If you enter 25
, the output will be:
Type of age_input: <class 'str'>
Notice that the type is <class 'str'>
, not <class 'int'>
. Because age_input
is a string ("25"
), you cannot directly perform mathematical operations on it like adding 1
. Trying to calculate age_input + 1
would result in a TypeError
, as Python doesn't know how to add an integer to a string in this context.
If you need to treat the user's input as a number (integer or float) or another data type, you must explicitly convert the string returned by input()
. You can use the type conversion functions we discussed earlier, such as int()
or float()
.
Here's how you can correctly handle numerical input:
age_str = input("Enter your age: ")
age_int = int(age_str) # Convert the input string to an integer
next_year_age = age_int + 1
print("Next year, you will be", next_year_age)
# You can also combine input and conversion in one step:
height_str = input("Enter your height in meters: ")
height_float = float(height_str) # Convert the input string to a float
print("Your height is:", height_float, "meters")
In this revised example:
input()
and store it in age_str
.int(age_str)
to convert the string "25"
(or whatever the user entered) into the integer 25
, storing it in age_int
.age_int + 1
.float()
for potential decimal values.Remember that if the user enters text that cannot be converted to the target type (e.g., typing "hello"
when int()
is used), the program will raise a ValueError
. Handling such errors robustly is covered later when we discuss exception handling.
Getting user input is fundamental for creating interactive programs. The input()
function provides the mechanism, but always keep in mind that its return value is a string, often requiring explicit type conversion before you can use it in calculations or comparisons.
© 2025 ApX Machine Learning