Multiple lines user Input in Python

avatar
Borislav Hadzhiev

Last updated: Feb 20, 2023
3 min

banner

# Table of Contents

  1. Multiple lines user Input in Python
  2. Read user Input until EOF in Python
  3. Read user Input until EOF using try/except

# Multiple lines user Input in Python

To take multiple lines of user input:

  1. Use a while loop to iterate for as long as the user is typing in values.
  2. On each iteration, append the user input and a newline character to a list.
  3. If the user presses Enter without typing in a value, break out of the loop.
main.py
lines = [] while True: user_input = input() # ๐Ÿ‘‡๏ธ if user pressed Enter without a value, break out of loop if user_input == '': break else: lines.append(user_input + '\n') # ๐Ÿ‘‡๏ธ prints list of strings print(lines) # ๐Ÿ‘‡๏ธ join list into a string print(''.join(lines))

user input multiple lines

We used a while loop to iterate for as long as the user is typing in values.

On each iteration, we check if the user pressed Enter without typing in a value to exit out of the while True loop.

This can be any other condition, e.g. you might have a stop word such as done or quit.

The break statement breaks out of the innermost enclosing for or while loop.

If the user types in a value, we use the list.append() method to append the value and a newline character to a list.

The list.append() method adds an item to the end of the list.

# Read user Input until EOF in Python

Alternatively, you can use the sys.stdin.readlines() method to read user input until EOF.

The readlines() method will return a list containing the lines.

The user can press CTRL + D (Unix) or CTRL + Z (Windows) to exit.

main.py
import sys # ๐Ÿ‘‡๏ธ User must press Ctrl + D (Unix) or Ctrl + Z (Windows) to exit print('Press CTRL + D (Unix) or CTRL + Z (Windows) to exit') user_input = sys.stdin.readlines() # ๐Ÿ‘‡๏ธ get list of lines print(user_input) # ๐Ÿ‘‡๏ธ join the list items into a string print(''.join(user_input))

read input until eof

stdin is used for interactive user input.

The user must press CTRL + D (on Unix) or CTRL + Z on Windows to exit.

The readlines() method returns a list containing the list the user entered.

You can use the str.join() method if you need to join the list of strings into a string.

main.py
# a # b # c print(''.join(['a\n', 'b\n', 'c\n']))

If you only need a string containing the lines, use the sys.stdin.read() method instead.

main.py
import sys user_input = sys.stdin.read() print(user_input)

read input until eof stdin read

The sys.stdin.read() method returns a string containing the lines the user entered.

Alternatively, you can use a try/except statement.

# Read user Input until EOF using try/except

This is a three-step process:

  1. Use a while loop to iterate until EOF.
  2. On each iteration, append the user input to a list.
  3. Catch the EOFError exception in the except block and break out of the loop.
main.py
lines = [] while True: try: lines.append(input()) except EOFError: lines_str = '\n'.join(lines) print(lines_str) break print(lines)

read user input until eof try except

We used a while loop to iterate until EOF.

If the user presses CTRL + D (Unix) or CTRL + Z (Windows), an EOFError exception is raised and is handled in the except block.

The break statement breaks out of the innermost enclosing for or while loop.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev