Getting User Input in Python

This post covers the Python input() function, explaining how to collect user input, why it always returns a string, and how to convert that input to the correct data type. A practical example ties the concepts together using f-strings for clean output.

Getting User Input in Python

One of the first things you want your programs to do is interact with the user. Instead of hardcoding values directly into your code, you can ask the user for input and use whatever they provide. In Python, this is done with a single built-in function: input().

The input() Function

The input() function pauses your program, displays a prompt to the user, waits for them to type something and press Enter, and then returns what they typed as a string. Here is the simplest possible example:

name = input("What is your name? ")
print("Hello, " + name + "!")

When you run this, Python prints the prompt, waits for the user to type, and then stores the result in the variable name. The output might look like this:

What is your name? Alice
Hello, Alice!

The string you pass into input() is the prompt the user sees. You do not have to include a prompt at all, but it is always good practice so users know what to type.

Everything Comes Back as a String

This is the most important thing to understand about input(): it always returns a string, no matter what the user types. If a user types the number 25, Python receives it as the string "25", not the integer 25.

This matters the moment you try to do any math with the result. Try this example:

age = input("How old are you? ")
print(age + 5)

This will crash with a TypeError because Python cannot add a string and an integer. To fix it, you need to convert the input using int() or float():

age = int(input("How old are you? "))
print("In 5 years you will be", age + 5)

Now the conversion happens right away, and the math works correctly.

Handling Different Data Types

Depending on what you need from the user, you will reach for different conversion functions:

  • int(): converts input to a whole number, such as int(input("Enter a number: "))
  • float(): converts input to a decimal number, such as float(input("Enter a price: "))
  • str(): the default, so no conversion is needed when you just want text

A Practical Example

Here is a small program that pulls all of this together. It asks for a user's name and their age, then prints a personalised message:

name = input("Enter your name: ")
age = int(input("Enter your age: "))
years_to_100 = 100 - age

print(f"Hi {name}! You have {years_to_100} years until you turn 100.")

Notice the use of an f-string (formatted string literal) in the final print() call. This is a clean way to embed variables directly inside a string by placing an f before the opening quote and wrapping variable names in curly braces. It is much easier to read than concatenating strings with the + operator.

A Word on Validation

You may have already thought: what happens if the user types "hello" when your program expects a number? The int() conversion will throw a ValueError and crash. Handling that gracefully with error checking is an important next step, and it involves a concept called try/except. That is covered in a dedicated post on error handling, so do not worry about it just yet. For now, practise assuming your users will enter valid data.

What's Next

Now that your programs can accept input, they need to make decisions based on that input. The next post covers conditional statements in Python, specifically if, elif, and else. This is where your programs start to branch and behave differently depending on what the user provides, which is where things really start to get interesting.