Get user input with Python by leveraging the versatile input()
function. With this function, users can effortlessly provide input through their keyboard directly into the console. In this tutorial, we will delve deep into understanding how to efficiently obtain keyboard input and explore its nuances.
It’s important to 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.
Related Course: Complete Python Programming Course & Exercises
Delving into the Syntax of the input() Function
The basic structure of the input()
function is demonstrated below:
input("Your name: ") |
The function optionally accepts a string parameter, often referred to as the prompt
. This string is displayed on the console to guide the user regarding what kind of information they should provide.
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
Below is a basic example that captures user input and then showcases it in the console:
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.
You can also employ formatted strings or f-strings
for enhanced output presentation. Simply prepend the string with an f
and use curly brackets to integrate your variables:
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 utilize 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.
Related Course: Complete Python Programming Course & Exercises