Open In App

Take input from stdin in Python

Last Updated : 09 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will read How to take input from stdin in Python.

There are a number of ways in which we can take input from stdin in Python.

Read Input From stdin in Python using sys.stdin

First we need to import sys module. sys.stdin can be used to get input from the command line directly. It used is for standard input. It internally calls the input() method. Furthermore, it, also, automatically adds ‘\n’ after each sentence.

Example: Taking input using sys.stdin in a for-loop

Python3




import sys
 
for line in sys.stdin:
    if 'q' == line.rstrip():
        break
    print(f'Input : {line}')
 
print("Exit")


Output 

 

Read Input From stdin in Python using input()

The input() can be used to take input from the user while executing the program and also in the middle of the execution.

Python3




# this accepts the user's input
# and stores in inp
inp = input("Type anything")
 
# prints inp
print(inp)


Output:

 

Read Input From stdin in Python using fileinput.input()

If we want to read more than one file at a time, we use fileinput.input(). There are two ways to use fileinput.input(). To use this method, first, we need to import fileinput.

Example 1: Reading multiple files by providing file names in fileinput.input() function argument

Here, we pass the name of the files as a tuple in the “files” argument. Then we loop over each file to read it. “sample.txt” and “no.txt” are two files present in the same directory as the Python file.

Python3




import fileinput
 
with fileinput.input(files=('sample.txt', 'no.txt')) as f:
    for line in f:
        print(line)


Output: 

 

Example 2: Reading multiple files by passing file names from command line using fileinput module

Here, we pass the file name as a positional argument in the command line. fileargument parses the argument and reads the file and displays the content of the file.

Python3




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


Output: 

 



Previous Article
Next Article

Similar Reads

Difference between input() and sys.stdin.readline()
Python is a widely used general-purpose language that can be used for many purposes. Taking input in any language is as important as breathing for humans. Python provides various methods for taking input. However, we all may get confused about how each method is different from one another. In this article, we will discuss about two such methods i.e
2 min read
Take Matrix input from user in Python
Matrix is nothing but a rectangular arrangement of data or numbers. In other words, it is a rectangular array of data or numbers. The horizontal entries in a matrix are called as 'rows' while the vertical entries are called as 'columns'. If a matrix has r number of rows and c number of columns then the order of matrix is given by r x c. Each entrie
3 min read
How to take integer input in Python?
In this post, We will see how to take integer input in Python. As we know that Python's built-in input() function always returns a str(string) class object. So for taking integer input we have to type cast those inputs into integers by using Python built-in int() function. Let us see the examples: Example 1: C/C++ Code # take input from user input_
2 min read
Take input from user and store in .txt file in Python
In this article, we will see how to take input from users and store it in a .txt file in Python. To do this we will use python open() function to open any file and store data in the file, we put all the code in Python try-except block. Let's see the implementation below. Stepwise Implementation Step 1: First, we will take the data from the user and
2 min read
How to Take Only a Single Character as an Input in Python
Through this article, you will learn how to accept only one character as input from the user in Python. Prompting the user again and again for a single character To accept only a single character from the user input: Run a while loop to iterate until and unless the user inputs a single character.If the user inputs a single character, then break out
3 min read
Python | Pandas TimedeltaIndex.take()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas TimedeltaIndex.take() function return a new Index of the values selected by the indices. We generally pass a list of indices to b
2 min read
Python | Pandas Series.take()
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.take() function return the elements in the given positional indices along an ax
3 min read
Python | Numpy matrix.take()
With the help of Numpy matrix.take() method, we can select the elements from a given matrix by passing the parameter as index value of that element. It will return a matrix having one dimension. Remember it will work for one axis at a time. Syntax : matrix.take(index, axis) Return : Return matrix of selected indexes Example #1 : In this example we
1 min read
How to take screenshot using Selenium in Python ?
Selenium offers a lot of features and one of the important and useful feature is of taking a screenshot. In order to take a screenshot of webpage save_screenshot() method is used. save_screenshot method allows user to save the webpage as a png file. Syntax : driver.save_screenshot("image.png") Argument : filename or the full path you wish to save y
1 min read
Create a Python Script Notifying to take a break
We generally do not take breaks when we are using our laptop or PC. It might affect our eyesight as well as mind. So with Python, we can make a program that can notify us that we have to take a break start again after sometime when the user again starts working on the laptop. Modules neededpyttsx3 - It is a text-to-speech conversion library in Pyth
4 min read
Practice Tags :
three90RightbarBannerImg