How to Read From stdin in Python

February 7, 2023

Introduction

Reading from stdin (standard input) in Python is a common programming task that helps create interactive I/O programs. Stdin reads keyboard inputs from a user, files, or data streams.

Python offers several different methods to read from stdin depending on the type of problem.

This guide provides three methods to show how to read standard input from stdin in Python.

How to Read From stdin in Python

Prerequisites

Method 1: Read From stdin Using sys.stdin

The sys module contains a stdin method for reading from standard input. Use this method to fetch user input from the command line.

Import the sys library and use the stdin method in a for loop. See the example below for a demonstration:

import sys

print('Write a message and press Enter: ')

for line in sys.stdin:
    line = line.rstrip()
    print(f'Message from stdin: {line}')
    break
sys.stdin standard input code and terminal output

The code does the following:

  • Reads user input from the keyboard (sys.stdin).
  • Removes new lines from the user entry (rstrtip()).
  • Prints back the entered message by appending an f-string.
  • Exits the for loop (break).

The program expects user input, prints the entered text stream, and returns to the command line.

Method 2: Read From stdin Using the input() function

The second way to read input from stdin in Python is with the built-in input() function. Enter a descriptive message as a value, and save the output into a variable.

For example:

message = input('Enter a message:')
print(f'Your name is: {message}')
input() method standard input code and terminal output

The program saves the input from stdin into a variable (name) and returns the value.

Method 3: Read from stdin by Using fileinput.input()

The fileinput Python library contains the input() function, which allows reading file names from standard input.

The method allows passing file names as a function or command-line arguments. In both cases, the file's contents are read as input from stdin. Below is a demonstration of both use cases.

Files as Function Arguments

To pass file names as the fileinput.input() arguments, the files must be located in the same directory as the script.

For example, if the current directory contains two files (file1.txt and file2.txt), use the following code to print the file contents line by line:

import fileinput

with fileinput.input(files=('file1.txt', 'file2.txt')) as f:
    for line in f:
        print(line)
fileinput.input function arguments code and terminal output

The with statement helps open a file stream, uses the file names as the function's input, then goes through each line in the file using a for loop. As a result, the contents of the files print to the console.

Files as Command Line Arguments

An alternative way to read files is to provide the file names as command-line arguments. Use this method when reading different files each time.

The following code shows how to read files provided as command-line arguments:

import fileinput
 
for f in fileinput.input():
    print(f)

Save the code and run with the following:

python3 test.py file1.txt file2.txt
fileinput.input command line arguments code and terminal output

The code goes through the list of files and prints the file contents to the console.

Conclusion

You now know three different methods to read input from stdin in Python. Standard input is an essential programming skill to get a user's input into a program.

Next, learn about file handling in Python or see how to take user input and create a calculator with Python.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP who is passionate about programming. Her background in Electrical Engineering and Computing combined with her teaching experience give her the ability to easily explain complex technical concepts through her content.
Next you should read
How to Read Files Line by Line in Bash
March 17, 2022

This tutorial shows how to read a file line by line using the Bash shell. See five different methods and process a file's contents...
Read more
File Handling in Python: Create, Open, Append, Read, Write
February 24, 2022

Working with files is part of everyday tasks in programming. This tutorial teaches you elementary file...
Read more
How to Make a Calculator With Python
February 1, 2023

This step-by-step guide shows how to make a calculator with Python and runs through some basic programming concepts. By the end of this tutorial...
Read more
How to PrettyPrint a JSON File with Python?
November 15, 2022

Learn how you can use Python to PrettyPrint a JSON file. PrettyPrinting helps reformat the JSON file into a readable and...
Read more