Python Getting User Input
Table of Contents
In the last lesson, we learned type conversion. Now let’s learn how to read input from a user.
After learning Python Type Conversion, we can turn text into numbers and other useful types. Now let’s accept values from the person using the program. In this tutorial, we’ll learn about Python user input with easy examples.
Let’s Talk to the User
So far, our programs just do things on their own. They print stuff. They do math. But they don’t talk to anyone. That’s fine for learning. But real programs need to interact with people. They need to ask questions. And get answers. That’s where input() comes in.
What Is the Python input() Function?
input() is a function that lets our program ask the user a question. And then it waits for the user to type something.
Think of it like this. Our program is a person. It asks, “What is your name?” And then it waits. The user types something. And the program gets that answer. Simple.
How to Use Python input()
Here’s a basic example.
name = input("What is your name? ")print("Hello,", name)When we run this, we see:
What is your name?The program is waiting. We type something, like Alex, and press Enter.
Then we see:
Hello, AlexSee what happened? The program asked a question. We gave an answer. The program used our answer. That’s input().
What Does Python input() Give Us?
This is very important. Let’s listen carefully.
input() always gives us a string. Always. Even if the user types a number. Even if they type True. It’s always a string.
age = input("How old are you? ")print(type(age))If we type 25, the output is:
<class 'str'>It’s a string. Not a number. So if we try to do math with it, things break.
age = input("How old are you? ")next_year = age + 1This gives an error. Because we can’t add a string and a number.
So what do we do? We convert the string to a number.
age = int(input("How old are you? "))next_year = age + 1print("Next year you will be", next_year)Now it works. This is a very common beginner mistake. Let’s remember: input() gives us a string. Always.
The Prompt in Python input()
The text we put inside input() is called the prompt. It’s what the user sees.
name = input("Enter your name: ")The prompt here is "Enter your name: ". We can put anything we want in the prompt. Just make sure it’s clear. The user should know what we’re asking for.
Notice the space at the end of the prompt. That’s important. It separates the prompt from what the user types.
Without the space:
Enter your name:AlexWith the space:
Enter your name: AlexSmall thing. But it makes a difference.
A Simple Python Input Program
Let’s build a small program that asks for some information and then prints it.
name = input("What is your name? ")age = int(input("How old are you? "))city = input("Which city do you live in? ")
print()print("Here is your information:")print("Name:", name)print("Age:", age)print("City:", city)When we run this, it will ask three questions. Then it will print a summary.
Example output:
What is your name? AlexHow old are you? 25Which city do you live in? London
Here is your information:Name: AlexAge: 25City: LondonThat’s a real program. It talks to the user. It gets information. It uses it.
Converting Python Input
Remember, input() gives a string. So we need to convert it if we want a number. Here’s how to convert to different types.
1 To Integer
age = int(input("Enter your age: "))To Float
price = float(input("Enter the price: "))To String (No Conversion Needed)
name = input("Enter your name: ")Since input() already gives a string, we don’t need to convert it.
Handling Errors in Python Input
What happens if the user types something wrong? For example, if we ask for an age and they type hello.
age = int(input("Enter your age: "))This will give an error:
ValueError: invalid literal for int() with base 10: 'hello'The program crashes. Not good. In later articles, we’ll learn how to handle errors gracefully. For now, let’s just know that this can happen. And it’s normal. Real programs check for these things.
One simple thing we can do is use strip() to remove extra spaces.
name = input("Enter your name: ").strip()This removes spaces from the beginning and end. So if the user accidentally types Alex, it becomes Alex. That’s helpful.
Python Input: A Real-Life Example
Think of input() like a conversation. We ask a question. The other person answers. We use their answer.
In Python, input() is us asking the question. The user typing the answer. And the variable storing the answer.
It’s that simple.
Common Mistakes with Python Input
Mistake 1: Forgetting to Convert
age = input("Enter your age: ")next_year = age + 1 # Error!So what do we do? We convert the input to a number.
age = int(input("Enter your age: "))next_year = age + 1Mistake 2: Converting to the Wrong Type
price = int(input("Enter the price: "))If the user types 19.99, this gives an error. Because 19.99 is not an integer.
So what do we do? We use float() instead.
price = float(input("Enter the price: "))Mistake 3: Not Using a Clear Prompt
x = input()What is the user supposed to type? They have no idea.
So what do we do? We use a clear prompt.
name = input("Enter your name: ")Mistake 4: Forgetting the Space at the End
name = input("Enter your name:")The user sees:
Enter your name:AlexSo what do we do? We add a space at the end.
name = input("Enter your name: ")Mistake 5: Using input() in Python 2
If we’re using Python 2 (which we shouldn’t), input() behaves differently. It evaluates the input as code. That’s dangerous. In Python 3, input() always returns a string. That’s safe. So let’s always use Python 3. And always use input().
Next, we will learn about f-Strings so we can continue step by step.