Python input() Function Explained

The input() function lets your Python program receive text from the user.

It is one of the most common beginner tools in Python because it makes programs interactive. You can use it to ask for a name, an age, a number, or any other value the user types.

Quick example

name = input("Enter your name: ")
print("Hello, " + name)

Use input() to get text from the user. It always returns a string.


What input() does

input() pauses the program and waits for the user to type something.

Here is what happens:

  • input() waits for the user to type
  • It reads text from the keyboard
  • It returns the typed value as a string
  • The program continues after the user presses Enter

Example:

color = input("What is your favorite color? ")
print("Your favorite color is", color)

Possible output:

What is your favorite color? blue
Your favorite color is blue

In this example:

  • The prompt is What is your favorite color?
  • The user's answer is stored in color
  • That value is then printed

Basic syntax

The basic syntax is:

input(prompt)

Important points:

  • The prompt is optional
  • The prompt appears before the user types
  • You usually store the result in a variable

Example with a prompt:

username = input("Enter your username: ")
print(username)

Example without a prompt:

value = input()
print(value)

You can use input() without a prompt, but a clear prompt is usually better. It helps the user know what to enter.


What input() returns

input() always returns a string.

That is true even if the user types a number.

Example:

age = input("Enter your age: ")
print(age)
print(type(age))

Possible output:

Enter your age: 25
25
<class 'str'>

Even though the user typed 25, Python treats it as text.

If you need a number, convert it with int() or float().

Example:

age = int(input("Enter your age: "))
print(age)
print(type(age))

Possible output:

Enter your age: 25
25
<class 'int'>

Simple examples beginners need

Ask for a name and print it

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

This stores the user's answer in name and prints it.

Ask for age and convert it with int()

age = int(input("How old are you? "))
print("Next year you will be", age + 1)

This works because int() converts the text from input() into a whole number.

If you want more help with this step, see how to convert user input to numbers in Python.

Ask for two numbers and add them after conversion

num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
total = num1 + num2

print("Total:", total)

Possible output:

Enter the first number: 2
Enter the second number: 3
Total: 5

If you skip the conversion, Python joins the values as strings instead of adding them as numbers.

Example:

num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")

print(num1 + num2)

Possible output:

Enter the first number: 2
Enter the second number: 3
23

That happens because "2" + "3" creates "23".


Common beginner problems

Beginners often run into the same few issues with input().

Forgetting that input() returns a string

This is the most common mistake.

value = input("Enter a number: ")
print(type(value))

Output:

<class 'str'>

If you need a number, convert it first.

Trying to add numbers without converting them

This gives string joining, not math.

a = input("Enter a number: ")
b = input("Enter another number: ")
print(a + b)

If the user enters 4 and 5, the result is 45, not 9.

Getting ValueError when converting invalid text to int()

Example:

age = int(input("Enter your age: "))

If the user types ten, Python raises a ValueError.

If you see this problem, read ValueError: invalid literal for int() with base 10.

Leaving extra spaces in user input

Users sometimes type extra spaces before or after their answer.

name = input("Enter your name: ")
print(repr(name))

If the user enters spaces, those spaces become part of the string.

You can remove them with .strip():

name = input("Enter your name: ").strip()
print(name)

Helpful tips

These habits make input() easier and safer to use.

  • Use clear prompts so users know what to enter
  • Use .strip() to remove extra spaces when needed
  • Validate input before using it in larger programs
  • Use try-except when converting input to numbers

Example with .strip():

city = input("Enter your city: ").strip()
print("City:", city)

Example with try-except:

try:
    age = int(input("Enter your age: "))
    print("You will be", age + 1, "next year")
except ValueError:
    print("Please enter a whole number.")

This prevents the program from crashing when the user enters invalid text.

If you are new to interactive programs, see how to get user input in Python.


Common mistakes

These are the most common causes of problems when using input():

  • Treating user input as a number without using int() or float()
  • Concatenating strings and numbers together incorrectly
  • Entering non-numeric text when the program expects a number
  • Assuming pressing Enter gives a number instead of an empty string

Useful debugging checks:

print(value)
print(type(value))
print(repr(value))

These help you inspect what input() actually returned:

  • print(value) shows the value
  • print(type(value)) shows the data type
  • print(repr(value)) shows hidden spaces and special characters

Example:

value = input("Enter something: ")
print(value)
print(type(value))
print(repr(value))

FAQ

Does input() return an integer?

No. input() always returns a string. Convert it with int() if needed.

Is the prompt required in input()?

No. You can call input() with no prompt, but prompts help users know what to type.

Why does 2 + 3 not work correctly with input()?

Because input() returns strings. "2" + "3" becomes "23" unless you convert both values to numbers.

How do I remove spaces from input?

Use .strip() on the returned string, such as input("Name: ").strip().


See also