The input() function reads what a user types on the keyboard. What you get back is always a string, and that is the part that surprises most beginners.
Note that older Python versions employed the raw_input() function, which is now obsolete.
In this article, we aim to:
- Understand the process of capturing input from both users and systems in Python.
- Recognize and accept different types of keyboard inputs, such as integers, floats, and strings.
- Dive into advanced output formatting techniques.
Looking at the Syntax of the input() Function
The basic structure of the input() function is demonstrated below:
input("Your name: ") |
The string in input('...') is the prompt. It is printed first, so the user knows what to type.
The input() function allows you to:
- Obtain user input directly from the keyboard using input().
- Wait for the user to press the enter key after providing their input.
- Ensure that the function will indefinitely wait for user input without any timeout.
- Retrieve the user input as a string, which you can then assign to a variable for further use.
- Exit the input phase using shortcuts like Ctrl-D (Unix) or Ctrl-Z+Return (Windows).
Capturing User Input in Python: A Simple Demonstration
Here is a complete example:
name = input("Please provide your name: ") |
The input() function obtains the user’s input and assigns it to the name variable. The print() function then displays the provided input.

To summarize, the input() function is an invaluable tool to capture textual data from users. But remember:
Tip: Always store the input data in a variable, like
var = input(), for further processing.
To print the value next to some text, use an f string:
name = input("Please enter your name: ") |

Type of Return from the input() Function
By default, the input() function stores data as a string (str). You can verify this by using the type() function.
When it comes to numbers, they aren’t stored as strings. If you intend to use them as integers or floats, you’ll need to explicitly convert them.
Accepting Integers as User Input
If you require an integer (a whole number) as input, you can easily convert the string data using the int() function:
x = int(input("Please provide a whole number for x: ")) |

Bear in mind that if the user enters a non-integer value, your code might encounter an error. Thus, always ensure the provided input is valid.
Reading Float Inputs
If you’re aiming to capture a floating-point number (non-integer), you can use the float() function to convert the string:
x = float(input("Please provide a number: ")) |
Exception Handling for Inputs
Users may sometimes provide unexpected or incorrect input. To tackle this, you can employ exception handling as shown below:
try: |
Legacy Method: raw_input()
For those using Python 2.x, the raw_input() function was the standard. However, with the advent of Python 3, input() has become the norm. If you’re still using older versions, consider updating for enhanced features and security.
Wrapping Up
The input() function is a powerful and efficient method to capture user input in Python. With its versatility, you can easily adapt it to your specific needs and scenarios. Whether you’re a newbie or a seasoned developer, mastering this function can add immense value to your coding toolkit.
Reading helps, writing fixes it. PyChallenge has exercises on this and you can try them right now.
