How To Get Input In Python

In this tutorial, we’ll cover how to get input from the user in Python. This can be useful in a variety of applications, from simple console-based games to more complex command-line tools. Using the built-in input() function, you can easily prompt the user for information and store it in a variable for later use.

Note: This tutorial assumes that you are using Python 3. If you’re using Python 2, the function to use is raw_input() instead of input().

Step 1: Basic Input

The simplest way to get input from the user is to use the input() function. This function pauses the program and waits for the user to enter some text, then returns that text as a string.

Here’s a basic example:

This code prompts the user for their name, then prints a personalized greeting.


What is your name? John
Hello, John!

Step 2: Converting Input to an Integer

If you want to get an integer input from the user, you’ll need to convert the string returned by input() using the int() function.

Here’s an example that demonstrates this:

This code prompts the user for their age, then calculates and prints how old they’ll be in five years.


How old are you? 25
In 5 years, you'll be 30 years old.

Step 3: Handling Invalid Input

Sometimes, users might enter invalid input, like a non-numeric value when you’re expecting a number. To handle this situation, you can use a tryexcept block to catch any exceptions thrown during input conversion.

Here’s an example:

This code attempts to convert the user’s input to a floating-point number. If the conversion fails (due to a ValueError), an error message is displayed instead of crashing the program.


How much do you weigh (in kilograms)? abc
Invalid input. Please enter a number.

Full Code


With the input() function and a few basic techniques, you can easily get input from users in Python. This powerful feature allows you to build interactive programs that are responsive and flexible.

Keep in mind that handling invalid input is important for a robust user experience, so make use of tryexcept blocks when working with user inputs.