AppDividend
Latest Code Tutorials

How to Take Input from Stdin in Python

0

The sys module in Python provides functions and variables used to modify the different parts of the runtime environment. The sys module provides sys.argv, sys.exit, sys.maxsize, sys.stdin, sys.stdout, sys.stderr, sys.path, and many different modules. The sys module provides functions to interact with the interpreter.

To work with a Python interpreter, you need to import the sys module in your program.

import sys

Take Input from Stdin in Python

To take the input from stdin Python, you can use one of the following ways.

  1. sys.stdin: It is used by the interpreter for standard input. 
  2. fileinput.input(): It is used to read more than one file at a time.
  3. input(): The input() function is used to take input from the user while executing the program.

Python sys.stdin

The sys module provides stdin, which can be used by the interpreter for standard input. What happens behind the scenes is that it internally calls the input() function. It automatically adds ‘\n’ after each sentence.

The sys.stdin is a file-like object on which you can call functions like read() or readlines() if you want to only read or read everything and split it by newline automatically.

Let’s create a Python program that takes input from the user using the sys.stdin module.

import sys

for inpt in sys.stdin:
    if 'Stop' == inpt.rstrip():
        break
    print(f'You input is: {inpt}')

print("You exited the program")

Output

krunal
You input is: krunal

simpson
You input is: simpson

stop
You input is: stop

Stop
You exited the program

In this example, we are reading the user inputs from the standard input and process it.

The rstrip() function removes the trailing newline character so that you can check if the user has entered the “Stopstring or not.

If the user input Stop then, it will exit the program. One thing to notice that if you input stop instead of Stop, then it won’t exit the program since sys.stdin is case sensitive, as you can see in the example.

Python fileinput.input()

Python fileinput.input() method takes the file as input and to can be used to update and append the data in the file. The fileinput.input() function is used if we want to read more than one file at a time.

Syntax

fileinput.input(files)

Arguments

It takes the files as an argument.

Example

To work with the fileinput.input() method, you need to import the fileinput module.

Let’s say I have a txt file in my current working directory called data.txt, and the content of that file is the following.

Yellow, This is Homer Simpson

Now, we will write a program using fileinput.input() method in which we will input the files in the command line, and it will return the content of the file. In our case, it is a data.txt file.

import fileinput

for line in fileinput.input():
    print(line)

To run this file, type the following command in your terminal.

python app.py data.txt

Yellow, This is Homer Simpson

And we get the content of the input data.txt file using fileinput.input() method.

The fileinput will loop through all the lines in the input specified as file names given in command-line arguments or the standard input if no arguments are provided.

Python input()

To get user input from the user in Python, use the input() method. The input() method reads a line from input, converts it into a string, and returns it.

If you are using Python2, then there is a raw_input() which exactly works as Python3‘s input() function.

Syntax

input(prompt)

Arguments

The input() method takes a prompt as an optional argument.

Example

simpson = input('What is your surname? ') 

print('Your surname is: ',simpson)

Output

What is your surname? Simpson

Your surname is:  Simpson

You can see that it asks for user input and when we hit the input, it will print it on the console. 

That is it for Python read from stdin tutorial with example.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.