Taking multiline input from a user in Python

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, depending on the specific requirements of your application, such as:

  • Reading a specific number of lines: Use this method when you know the exact number of lines the user will provide.
  • Reading until a terminator: This method allows the user to flexibly indicate the end of their input by using a specific terminator string (e.g., “END”).
  • Capturing input until an EOF signal: Choose this method when you want to allow users to terminate input with system-specific shortcuts (Ctrl+D or Ctrl+Z).

Each of these methods can be adapted and extended to meet the needs of your application, giving you more control over how you interact with users and process their input.

Let’s explore each method in more detail:

1. Reading a Specific Number of Lines

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.

The code below will prompt the user to enter three lines of text, storing each line in a list.

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?

A more Pythonic approach would be to use a list comprehension:

num_lines = 3  # Number of lines you want to read
lines = [input(f"Line {i + 1}: ") for i in range(num_lines)]
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?

2. Reading Until a Terminator

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’ll 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) 
Terminal
Enter your lines. Type 'END' to finish.
Hi
Hello
How are you?
END
You entered:
Hi
Hello
How are you?

For a more Pythonic solution, consider using a list comprehension with iter() function:

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

lines = [line.rstrip() for line in iter(input, 'END')]  # Assuming 'END' as the terminator
print("You entered:")
for line in lines:
    print(line) 
Terminal
Enter your lines. Type 'END' to finish.
Hi Hello How are you? END Your multiline input: Hi Hello How are you?

3. Capturing Input Until an EOF Signal

Sometimes, relying on the user to enter a specific terminator string to signal the end of their input might be less intuitive. In that case, capturing input until an EOF (End-of-File) signal is a valuable approach.

EOF stands for “End-of-File”. This technique leverages signals typically used when reading files, but you can adapt it for user input. The way to signal an EOF differs between operating systems:

  • Unix-based systems (like Linux and macOS): Press “Ctrl + D”
  • Windows: Press “Ctrl + Z” + Enter

To implement this, you’ll use a try-except block to catch the EOFError exception that input() raises when it encounters an EOF signal. Here’s how it’s done:

lines = []
print("Enter your text (Ctrl+D or Ctrl+Z then Enter to finish):")
try:
    while True:
        line = input()
        lines.append(line)
except EOFError:
    pass  # End of input
print("You entered:")
for line in lines:
    print(line)
Terminal
Enter your text (Ctrl+D or Ctrl+Z then Enter to finish):
Hi
Hello
How are you?
^Z
You entered:
Hi
Hello
How are you?