Take input from the user is an important part of any programming language. The output of the many programs depends on the standard input. The way of taking input from the user is different for different programming languages. Many ways exist in python to read from the standard input. The input() function is the most common way is to read from the standard input, which is a built-in function. The sys.stdin is another way is to read from the standard input the calls input() function internally. Python has another module named fileinput for reading the standard input. The input() function of this module can be 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 data from stdin by using input() function

The input() function is the most used function to take input from the user. Create a python file with the following script to take input from the user until the ‘n’ key is pressed. Here, an infinite loop is created by using the while loop. The first input() function is used to take the data from the user, and the print() function is used to print the input value. Next, the input() function of the script is used to ask the user to continue the task again or exit from the script. If the user presses ‘n’ or ‘N’, the loop’s iteration will be stopped by the break statement; otherwise, the loop will iterate again 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:

    # Take input from the user

    inputVal = input(“Type any text:n)

    # Print the input value

    print(“The input value is %s” %(inputVal))

    # Ask for next iteration

    nextInput = input(“Do you want to continue? (Y/N)”)

    # Terminate from the loop if ‘n’ is pressed

    if nextInput.upper() == ‘N’:

        break

# Print the termination message

print(“Program terminated.”)

Output:

The following similar output will appear after executing the above script. Here, ‘LinuxHint‘ has given as the first input value and terminated from the script for pressing the character, ‘n’.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image1-17.png" data-lazy- height="421" src="data:image/svg xml,” width=”1032″>

Example-2: Read data from stdin by using sys.stdin

The sys.stdin is another option of Python to take standard input from the users from the terminal. It calls the input() function internally and adds ‘n‘ after taking the input. Create a python file with the following script to check the use of the sys.stdin to take standard input. Here, the ‘for-in’ loop is used to take 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 ask the user to stop the script or not. 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 any text:”)

# Take input using stdin

for inputVal in sys.stdin:

    # Print the input value

    print(‘The input value is:%s’ % inputVal)

    # Ask for the next iteration

    nextInput = input(“Do you want to terminate? (Y/N)”)

    # Terminate 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 after executing the above script. Here, ‘Python Programming‘ has given as the first input value and terminated from the script for pressing the character, ‘y’.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image2-16.png" data-lazy- height="420" src="data:image/svg xml,” width=”1035″>

Example-3: Read data from stdin by using fileinput

The fileinput is another module of Python to take 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 as an argument value, it will take the input from the file. Create a python file with the following script to take standard input from the terminal. Here, the ‘for-in’ loop is used as the previous example 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 stop the script or not. The script will be terminated if the user types ‘quit’ or ‘Quit’ or ‘QUIT’. The upper() function is used here also 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 similar output will appear after executing the above script. Here, ‘Learn python from LinuxHint.com’ has given as the first 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 have to press ctrl d after taking the input.

You have to provide the filename as the argument value of the fileinput.input() function if you want to take data from the file instead of the terminal.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image3-16.png" data-lazy- height="440" src="data:image/svg xml,” width=”973″>

Conclusion:

Three different ways to take input from the terminal have been shown in this tutorial by using three simple examples. No module is required to use 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 take the standard input based on their requirements after reading this tutorial.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/channel-logo-150×150.jpg60eb765c64499.jpg" height="112" src="data:image/svg xml,” width=”112″>

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.