5 Ways of Taking Input in Python

Nice list of taking inputs in Python. Let me add on two more ways compatible with Python 3.10.

  1. Use stdin.readline for faster inputs
    Usage:
import stdin
string_input = stdin.readline().strip('\n')

Using readline from stdin is faster than the built-in input() function.

  1. Shorter multiline input
open(0)  # By default, 0 argument to open directs it to reading entire STDIN in a single go
multiline_integers = map(int, open(0))

Using open(0) to take inputs by means of file-handling function open() function allows shorter code that can be helpful in writing shorter code, for cases like Code Golfing.

Hoping that it adds some value to this thread. Happy Pythoning! :slight_smile:

1 Like