How to Read Input From Stdin in Python
Take input from the user is an important part of whatever programming linguistic communication. The output of the many programs depends on the standard input. The mode of taking input from the user is different for different programming languages. Many means exist in python to read from the standard input. The input() role is the nigh common manner is to read from the standard input, which is a built-in part. The sys.stdin is another fashion is to read from the standard input the calls input() function internally. Python has some other module named fileinput for reading the standard input. The input() role of this module tin exist used to read standard input or read content from one or more files. Different ways to read from the standard input in Python have been explained in this tutorial.
Exampe-1: Read information from stdin by using input() function
The input() function is the virtually used function to have input from the user. Create a python file with the following script to have input from the user until the 'n' key is pressed. Here, an space loop is created past using the while loop. The first input() office is used to have the information from the user, and the print() function is used to impress the input value. Adjacent, the input() function of the script is used to ask the user to continue the job again or get out from the script. If the user presses 'n' or 'N', the loop's iteration will be stopped past the break argument; otherwise, the loop will iterate once more and take another input from the user. The upper() function is used in the script to capitalize the value given by the user.
# Define an infinite loop
while True:
# Have input from the user
inputVal = input ( "Type whatever text:\due north" )
# Print the input value
print ( "The input value is %s" %(inputVal) )
# Ask for adjacent iteration
nextInput = input ( "Practise you lot desire to go on? (Y/N)" )
# Terminate from the loop if 'n' is pressed
if nextInput.upper ( ) == 'N':
break
# Impress the termination message
print ( "Program terminated." )
Output:
The post-obit similar output will announced later executing the higher up script. Here, 'LinuxHint' has given as the first input value and terminated from the script for pressing the character, 'n'.
Case-2: Read information from stdin past using sys.stdin
The sys.stdin is another pick of Python to take standard input from the users from the last. It calls the input() function internally and adds '\n' after taking the input. Create a python file with the post-obit script to check the use of the sys.stdin to take standard input. Hither, the 'for-in' loop is used to have the input from the user infinite times until the user wants to terminate the script. After printing the input value, the input() function is used to enquire the user to stop the script or non. The script will be terminated if the user presses 'y' or 'Y'. The upper() function is used here also to capitalize the input value.
# Import sys module
import sys
print ( "Type whatever text:" )
# Take input using stdin
for inputVal in sys.stdin:
# Print the input value
print ( 'The input value is:%s' % inputVal)
# Inquire for the adjacent iteration
nextInput = input ( "Do you want to stop? (Y/Due north)" )
# Finish from the loop if 'y/Y' is pressed
if nextInput.strip ( ).upper ( ) == 'Y':
break
else:
print ( "Type any text:" )
Output:
The following similar output will appear afterwards executing the above script. Here, 'Python Programming' has given equally the start input value and terminated from the script for pressing the character, 'y'.
Instance-3: Read data from stdin past using fileinput
The fileinput is another module of Python to have standard input. The lines of text can be taken from the terminal or a file by using fileinput.input(). If no argument value is provided in this function, it will take input from the terminal and if the name of an existing file is provided every bit an argument value, it will take the input from the file. Create a python file with the post-obit script to take standard input from the last. Here, the 'for-in' loop is used as the previous case to take input for infinite times until the user wants to terminate the script. Next, the input() function is used to ask the user to finish the script or not. The script will exist terminated if the user types 'quit' or 'Quit' or 'QUIT'. The upper() function is used here likewise to capitalize the input value. The strip() function is used to remove the extra spaces from both sides of the input value.
# Import fileinput module
import fileinput
print ( "Enter the text:" )
''' Take input using fileinput.input() function and press ctrl+D to finish taking the input value '''
for inputVal in fileinput.input ( ):
# Terminate from the loop if 'quit' is typed
if inputVal.strip ( ).upper ( ) == "QUIT":
break
# Print the input value
print ( "The input value is:" , inputVal)
print ( "Enter the text:" )
Output:
The following like output volition appear afterward executing the higher up script. Here, 'Learn python from LinuxHint.com' has given as the get-go input value and terminated from the script for typing the word, 'quit'. You have to remember one thing while taking input from the terminal using the fileinput module. That is, you accept to press ctrl+d after taking the input.
You take to provide the filename equally the argument value of the fileinput.input() part if you lot want to take data from the file instead of the terminal.
Determination:
Three unlike ways to accept input from the concluding have been shown in this tutorial by using three uncomplicated examples. No module is required to employ the input() function for taking the input. The sys module is required to import for using sys.stdin, and the fileinput module is required to import for using fileinput.input() in the script to take standard input. I hope the Python users will have the standard input based on their requirements after reading this tutorial.
Source: https://linuxhint.com/read-from-stdin-in-python/
0 Response to "How to Read Input From Stdin in Python"
Post a Comment