Python input() Function

Usage

The input() function allows you to prompt the user for some text input. This function is commonly used in console-based programs and scripts where interaction with the user is required.

Syntax

input(prompt)

Parameters

ParameterConditionDescription
promptOptionalA String which acts as a prompt to guide the user on what kind of input is expected.

Basic Example

The simplest form of input() is to call it without any arguments, which will prompt the user to input something and press Enter. However, to make your programs more user-friendly, you can pass a string to input(), which acts as a prompt to guide the user on what kind of input is expected.

For example, the code below asks the user for their name:

input("Enter your name: ")

When your Python program encounters the input() function, execution pauses. The program waits for the user to type something into the console and press the Enter key. Once Enter is pressed, the input() function captures all the text the user entered and returns it as a string.

You can conveniently store the string returned by input() in a variable for later use in your program. For example, if you want to personalize a greeting, you could ask the user for their name and then incorporate it into a friendly message:

name = input("Enter your name: ")
print(f"Hello, {name}!")
Terminal
Enter your name: Bob
Hello, Bob!

Please note that if the user signals the end-of-file (EOF) with Ctrl-D on Unix-like systems or Ctrl-Z followed by Enter on Windows, the input() function will raise an EOFError.

Working with Numbers

It’s important to remember that the input() function always treats the user’s input as a string, even if the user enters numbers, letters, or symbols. This means that if your program expects numerical input, you’ll need to convert the string into a numerical format explicitly.

Python provides functions like int() to convert strings to integers and float() to convert strings into floating-point numbers.

number = int(input("Enter an integer: "))
print("The integer is:", number)

float_number = float(input("Enter a floating point number: "))
print("The floating point number is:", float_number)
Terminal
Enter an integer: 5
The integer is: 5
Enter a floating point number: 3.14
The floating point number is: 3.14

Taking Multiline Input

Taking multiline input from a user in Python requires a bit more consideration than simply using the input() function for single lines of text.

There are several approaches to handle multiline input, such as:

If you know in advance how many lines of input you need from the user, you can simply call input() multiple times and concatenate or collect these lines into a list or a single string.

lines = []
for i in range(3):  # Read 3 lines of input
    line = input(f"Line {i + 1}: ")
    lines.append(line)

print("You entered:")
for line in lines:
    print(line) 
Terminal
Line 1: Hi
Line 2: Hello
Line 3: How are you?
You entered:
Hi
Hello
How are you?

Sometimes, you won’t know beforehand how many lines the user wants to provide. In this case, a flexible approach is to instruct the user to indicate the end of their input using a specific terminator string (e.g., “END”, “.”, etc.).

To implement this, you can use a loop that continuously reads lines until it encounters the specified terminator string. Here’s how it’s done:

lines = []
print("Enter your lines. Type 'END' to finish.")

while True:
    line = input()
    if line == "END":
        break
    lines.append(line)

print("You entered:")
for line in lines:
    print(line)

Security Considerations

Be cautious when using the input from input() directly in your code, especially if you’re passing it to functions that execute commands (e.g., eval(), exec()) or SQL queries. Always validate or sanitize user input to prevent security vulnerabilities such as code injection or SQL injection.