Python Read Input From Stdin Methods With Examples

This (stdin) is a standard input that is used as an interpreter to read input from the user.

In Python, to read input from stdin, it could simply be done by (sys module) you just need to import it.

This sys.stdin can be used to get input by the user directly from the (command line).

What is stdin Python?

The stdin is a Python standard input data in which the input() method is internally called by the function.

In addition, after each sentence (backslash N or ‘ \n') will be added automatically to a program.

How do you take stdin input in Python?

In Python, to take stdn input, you need to use the (sys module) which serves as an interpreter for standard input.

It will automatically add the input() print function.

Furthermore, the given input string will be appended with a newline character ('/n') at the end of each line.

For example:

import sys

for line in sys.stdin:

    if 'Exit' == line.rstrip():

        break

    print(f'Processing Message from sys.stdin *****{line}*****')

print("Done")

Output:

Input : I'm Glenn Magada Azuelo

Input : A passionate writer and computer programmer!!

Done


** Process exited - Return Code: 0 **
Press Enter to exit terminal

How to read input from stdin in Python?

Finally here are the methods to read input from Stdin in Python.

1. Read input from (stdin) or standard input using the input() method

In Python, the input() method can also be used to take input from the user while the program is executing, either from the start or in the middle of the execution.

For example:

# Glenn Magada Azuelo
# this accepts the user input
# and stores in inp

inp = input("Type your complete name:")

# prints inp
name = "Your complete name is: "
print(name+inp)

Output:

Type your complete name:
Glenn Magada Azuelo
Your complete name is: Glenn Magada Azuelo


** Process exited - Return Code: 0 **
Press Enter to exit terminal

2. Read input from stdin with the use of fileinput.input() module

The fileinput is a module that can write and read multiple files from stdin.

This will take the file as an argument, and the text present will return to it.

In addition, this module can also write and read other file formats and calls the input function.

For example:

import fileinput
 
for line in fileinput.input():

    print(line)

Output:

sample.txt

This a sample text file

3. Read from stdin with the use of sys.readline in Python

The sys module also offered a sys.readline() function.

This function can read the stdin stream line by line and also read an escape character.

In addition, this function also can read characters which set to parameters.

It means that we can set a function to read the specific number of characters input by the users.

For example:

import sys
   
firtsInput = sys.stdin.readline()
print(firtsInput)
   
secondInput = sys.stdin.readline(4)
print(secondInput)

Output:

Glenn Magada Azuelo
Glenn Magada Azuelo

Glenn Magada Azuelo

Glen


** Process exited - Return Code: 0 **
Press Enter to exit terminal

4. Read an input binary data from stdin in Python

At times, some programs may require reading a file that contains binary data.

In order for you to read this binary data, you will need to import the three modules, namely, (OS, SYS, and MSVCRT).

For example:

import os, sys, msvcrt
 
msvcrt.setmode (sys.stdin.fileno(), os.O_BINARY)
inputs = sys.stdin.read('sample.bin')
print len (inputs)

Output:

0x34, 0x2A, 0x69

5. Read stdin (standard input) until the specific character in Python

At times, there might be instances where you want to read standard input until you reach a specific character, just like other specific strings or an integer.

We can also use the (sys module) and we need to create a loop that terminates when we click or enter a specific character.

For example:

import sys
 
def read_until_character():
    x = [ ]
    minus = False
    while True:
        character = sys.stdin.read(1) 
        if not character: 
            break
        if character == '2' and minus:
            x.pop() 
            break
        else:
            minus = (character == '-')
            x.append(char)
    return ''.join(buf)
 
print(read_until_character())

Output:

21 11 44 -13 42 -11 2
21 11 44

How do you use the input function in Python?

The input() function in Python is used to get input from the user, such as characters or strings.

This input function will convert it into a string, and once you enter an integer value, the input() function will still convert it into a string.

How do you read a string in Python?

In Python, to read a string from a console program as an input, you can use the function named input().

This input() function can get an argument printed as a message to the console, where the user can prompt and be aware of what they’re expecting.

Summary

This article discusses how Python Read Input from Stdin.

It also provides the best explanation and sample program, such as how to read input from (stdin) or standard input using the input() method and read input from stdin with the use of fileinput.input() module, read from stdin using sys.readline, read input binary data from stdin, and read stdin (standard input) until the specific character.

You may also check the Python XOR in Simple Words with Examples.

I hope this article helped you learn a lot in your Python programming journey.

Leave a Comment