Skip to main content

How to Handle Multi-Line User Input in Python

Collecting multi-line user input is a common requirement in interactive Python applications. Whether you need to gather several lines of text or handle input until a specific signal (like EOF), mastering different input methods is essential.

This guide explores various techniques for handling multi-line user input in Python.

Collecting Multi-Line Input with a Loop

One approach to handling multi-line input is using a while loop combined with the input() function. You can control the loop based on user input to gather multiple lines.

lines = []

while True:
user_input = input()

if user_input == '':
break
else:
lines.append(user_input + '\n')

print(lines)
print(''.join(lines))

Explanation:

  • We initialize an empty list called lines.
  • The while True loop continuously prompts the user for input using input().
  • If the user presses Enter without typing any text, the input will be an empty string (''). In this case, the if statement executes a break to exit the loop.
  • If the user types text, the else statement executes, and we append the input string along with a newline character \n to the lines list, ensuring proper line separation.
  • Finally, the code prints the list of strings, followed by a single string with the multiple lines concatenated together using join.

This approach is flexible and allows you to customize the exit condition based on your needs (e.g., a specific "stop word").

Reading Input Until EOF using sys.stdin.readlines()

The sys.stdin.readlines() method offers a more direct way to read multi-line input until an End-Of-File (EOF) signal is encountered. The EOF signal is triggered by pressing Ctrl + D (Unix/macOS) or Ctrl + Z (Windows) in the terminal.

import sys

print('Press CTRL + D (Unix) or CTRL + Z (Windows) to exit')
user_input = sys.stdin.readlines()

print(user_input)
print(''.join(user_input))

Explanation:

  • We import the sys module, which provides access to system-specific parameters and functions, including standard input.
  • sys.stdin.readlines() reads all input lines from the terminal until an EOF signal is received and returns a list of strings, where each string represents a line of input.
  • The code prints this list and then joins the elements into a single string for display.
note

This method is ideal for capturing multi-line input from standard input, such as when piping data from another process.

Reading Input Until EOF using sys.stdin.read()

If you only need the input as one single string containing all the lines, the sys.stdin.read() method is a good fit. This method returns the entire input stream as a string.

import sys

user_input = sys.stdin.read()
print(user_input)

Explanation:

  • sys.stdin.read() reads the entire input stream until an EOF signal is received and returns it as a single string including newline characters.
  • The code prints this single string.
note

This is useful if you don't need the individual lines.

Reading Input Until EOF with Try/Except Blocks

You can also use a try/except block to handle input until EOF. In this case, the EOFError is captured when the EOF signal is sent.

lines = []

while True:
try:
lines.append(input())
except EOFError:
lines_str = '\n'.join(lines)
print(lines_str)
break
print(lines)

Explanation:

  • We start with an empty lines list.
  • The while True loop continuously attempts to get input using input().
  • When the user sends an EOF signal (Ctrl+D or Ctrl+Z), the input() function raises an EOFError exception.
  • The except EOFError block catches the exception.
  • We join the lines together with newline characters into a single string lines_str, and then print it.
  • The break statement is used to exit the loop.
  • Finally, the code prints the list of the input lines.
note

This approach provides a more explicit way to handle EOF and allows you to perform specific actions before exiting the loop.