Python Input Function

The Python input function is used to get input from the user. Whether it's asking for the user's name, age, or some other data, input makes it simple to interact with the user and receive their response during the program’s execution.

Here are some common ways to use it:

  • Asking for user input: Get responses like a user’s name or age.
  • Interactive programs: Make your program more interactive by accepting user responses.
  • Input for calculations: Take input from users to perform mathematical operations.


What You'll Learn

You will learn the basics of using the input function in Python to receive input from users. We’ll focus on the essentials, so you can begin writing interactive programs in no time!


Understanding the input Function

The basic syntax for the input function is:

python
input(prompt)
  • prompt: The message or question that is displayed to the user. It can be anything you want to ask the user.

The input function waits for the user to type something and press Enter. When the user presses Enter, the function returns the entered text as a string. If you need to convert this input to other data types (like an integer or float), you can use the int() or float() functions.


Example 1: Asking for the User's Name

To ask for the user’s name, use the input function and display a prompt message. The user will type in their name, and the program will store it as a string.

python
name = input("What is your name? ")
print(name)

How It Works:

  • input: This is the Python function used to get input from the user.
  • "What is your name?": This is the prompt message that will appear on the screen asking for the user's name.
  • The result is that Python will store whatever the user types as a string in the variable name.

Output

What is your name? John
John

Example 2: Asking for the User's Age

You can also use input to ask for a number, but remember that input is always returned as a string. If you want to work with numbers, you'll need to convert the input.

python
age = int(input("How old are you? "))
print(age)

How It Works:

  • input: This function gets input from the user.
  • "How old are you?": The prompt that will be displayed to the user.
  • int(): This converts the input string into an integer so you can perform math operations.
  • The user will type in their age, and the program will store it as an integer in the variable age.

Output

How old are you? 25
25

Example 3: Combining Input with Other Data

You can also combine user input with other strings and variables. For example, you could greet the user with their name and age in the same sentence.

python
name = input("What is your name? ")
age = int(input("How old are you? "))
print("Hello, " + name + ". You are " + str(age) + " years old.")

How It Works:

  • The program first asks for the user’s name and age using input.
  • The age input is converted into an integer using int().
  • The program then prints a greeting message using both the name and age, combining them with the + operator.

Output

What is your name? John
How old are you? 25
Hello, John. You are 25 years old.

Exercises

Try the following exercises to practice using the input function.

1. Write a program that asks for your favorite color.
python
# Exercise 1: Ask for your favorite color
color = input("What is your favorite color? ")
print("Your favorite color is " + color)

2. Write a program that asks for your age and prints a message based on it.
python
# Exercise 2: Ask for your age and print a message
age = int(input("How old are you? "))
if age >= 18:
    print("You are an adult.")
else:
    print("You are a minor.")

3. Write a program that asks for the user's favorite number and prints it.
python
# Exercise 3: Ask for a favorite number
number = int(input("What is your favorite number? "))
print("Your favorite number is " + str(number))

*Tip: Feel free to experiment with more advanced inputs and try combining different types of data!


Frequently Asked Questions

What does the input() function do in Python?

The input() function waits for user input and returns it as a string once the user presses Enter.


Is the input() function synchronous?

Yes, input() halts the program until the user types something and hits Enter — making it a blocking or synchronous call.


How do I convert input to an integer or float?

Wrap input() with int() or float() to convert the user’s input. For example: int(input("Enter a number: ")).


Can I use input() with multiple variables?

Yes! You can use .split() to unpack values. Example: x, y = input("Enter two numbers: ").split().


Is input() available in all Python versions?

The input() function is standard in Python 3. In Python 2, use raw_input() instead.



What’s Next?

Next, you'll learn about the print() function — a basic but powerful way to display output in Python.