In this Python tutorial, you will learn how do you read the input from stdin.
Table Of Contents
Let’s dive into the tutorial.
Read input from stdin using sys.stdin
The sys is a module available in python, which is used to interact with python interpreters. The variables present in the environment can be accessed by using the sys module.
The stdin object available in the sys module, is used to read the input from the python shell.
We have to iterate inside a for loop with stdin and continuously take read the input. To delete the new line character, we can use rstrip(). At one moment, based on the condition, the input reading is stopped, and program will exit.
- Remove special characters from a string in python
- How to Drop Index Column of a Pandas DataFrame
- Python | Add to Dictionary If Key doesn’t exist
- Pandas: Drop first N rows of dataframe
for iterator in sys.stdin: if (condition/expression iterator.rstrip():) other statements ......... ......... Exit the Program
Where the iterator is used to iterate the for loop for the given inputs with sys.stdin. The condition/expression will break taking inputs and exit the program. So if the condition/expression is false, it will continue taking inputs.
In this example, we will read inputs one by one, and if the input is equal to “python”, then the program is exited/stopped.
import sys # Take input in a loop for iterator in sys.stdin: # If input is 'python' then exit the program if 'python' == iterator.rstrip(): break # Display the input print('The Input given is: ',iterator) print("Program is stopped")
Welcome The Input given is: Welcome to The Input given is: to thisPointer The Input given is: thisPointer python Program is stopped
How did it work?
- First, we gave input as “Welcome”, which is not equal to “Python”. Hence it did not go inside the condition and continued iterating.
- Then we gave input as “to”, which is not equal to “Python”. Hence it did not go inside the condition and continued iterating.
- After we gave input as “thisPointer”, which is not equal to “Python”. Hence it did not go inside the condition and continued iterating.
- Finally, we gave input as “python”, which is equal to “Python”. Hence it did go inside if condition and break statement executed and program stopped.
Read input from stdin using input()
The input() function takes only single input. We can also use that to read the input in Python.
Here, message is the string which is used to explain what the input to be given is, It is optional.
In this example, we will take an input and display the input given.
# Enter any input take_input = input("Enter: ") # display the entered input print(take_input)
Enter: welcome to thispointer welcome to thispointer
Here, our input is welcome to thispointer. So it got displayed. We can also typecast this input into integer, float, etc.
Integer - int(input()) Float - float(inout())
In this example, we will take one integer input and float input and add these two values.
# enter integer input take_input1 = int(input("Enter an Integer value: ")) # enter float input take_input2 = float(input("Enter a float value: ")) # add the two inputs print(take_input1 + take_input2)
Enter an Integer value: 55 Enter a float value: 78.5 133.5
In the above code, we entered the integer value as 55 and the float value as 78.5. Then we performed the addition of these two inputs.
Read input from stdin using fileinput.input()
In this scenario, we will take a file as input. It can be possible to take multiple files at a time as input. But we have to import the fileinput module.
tupleOfFiles = ('filename1.extension','filename2.extension',............'filenamen.extension') with fileinput.input(files = tupleOfFiles) as file_pointer: some statements ............... ...............
Using with keyword, we can implement this type of input. The input takes one parameter – files. It can be a tuple of file names with an extension that makes these files as inputs.
In this example, we will read a file named tutorial.txt and display all the contents present in that file using file_pointer through for loop.
import fileinput # Take tutorial.txt file as input file with fileinput.input(files = ('tutorial.txt')) as file_pointer: # Iterate data from file for iterator in file_pointer: # Display line from a file print(iterator.strip())
This is the first line. This is the second line. This is the third line. This is the fouth line. This is the fifth line.
The contents present in the tutorial.txt file are displayed.
In this example, we will read two files named tutorial1.txt and tutorial2.txt. Then we will display all the contents present in these files using file_pointer in a for loop.
- The content in tutorial1.txt is Hello Python
- The content in tutorial2.txt is Hello C++
import fileinput # Read in put from tutorial1.txt and tutorial2.txt with fileinput.input(files = ('tutorial1.txt','tutorial2.txt')) as file_pointer: # Iterate data from files for iterator in file_pointer: # Display a line print(iterator.strip())
Hello Python Hello C++
We can see that the contents in both the files are displayed one by one.
From the above tutorial, we learned three ways to get input from stdin in Python. If you want to read the input through a file, then you prefer the fileinput.input() method. In other cases, you can use either input() or sys.stdin. Happy Learning.