Reads a String From the Standard Input Python

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 nearly common way is to read from the standard input, which is a built-in function. The sys.stdin is another fashion is to read from the standard input the calls input() part 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 ane or more files. Different ways to read from the standard input in Python accept been explained in this tutorial.

Exampe-1: Read data from stdin by using input() function

The input() role is the most used function to have input from the user. Create a python file with the following script to accept input from the user until the 'due north' key is pressed. Here, an space loop is created by using the while loop. The first input() role is used to take the data from the user, and the print() function is used to print the input value. Next, the input() role of the script is used to ask the user to continue the task once again or exit from the script. If the user presses 'n' or 'N', the loop's iteration volition be stopped past the interruption statement; otherwise, the loop will iterate over again and take another input from the user. The upper() office 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

impress ( "The input value is %south" %(inputVal) )

# Ask for next iteration

    nextInput = input ( "Practice you want to go along? (Y/Northward)" )

# End from the loop if 'n' is pressed

if nextInput.upper ( ) == 'Due north':

break

# Print the termination message

print ( "Programme terminated." )

Output:

The following similar output will appear after executing the in a higher place script. Here, 'LinuxHint' has given every bit the outset input value and terminated from the script for pressing the graphic symbol, 'n'.

Instance-2: Read information from stdin by using sys.stdin

The sys.stdin is some other option of Python to accept standard input from the users from the terminal. It calls the input() function internally and adds '\n' subsequently 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 space times until the user wants to terminate the script. Later printing the input value, the input() function is used to ask the user to cease the script or not. The script will be terminated if the user presses 'y' or 'Y'. The upper() function is used here too to capitalize the input value.

# Import sys module

import sys

print ( "Type any text:" )

# Take input using stdin

for inputVal in sys.stdin:

# Impress the input value

print ( 'The input value is:%south' % inputVal)

# Ask for the next iteration

    nextInput = input ( "Exercise yous want to cease? (Y/N)" )

# End from the loop if 'y/Y' is pressed

if nextInput.strip ( ).upper ( ) == 'Y':

break

else:

print ( "Type whatever text:" )

Output:

The following like output will appear subsequently executing the above script. Here, 'Python Programming' has given as the outset input value and terminated from the script for pressing the character, 'y'.

Case-3: Read data from stdin past using fileinput

The fileinput is another module of Python to take standard input. The lines of text can be taken from the last 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 statement value, it will take the input from the file. Create a python file with the following script to have standard input from the terminal. Hither, the 'for-in' loop is used equally the previous example to take input for infinite times until the user wants to terminate the script. Side by side, the input() function is used to inquire the user to stop the script or non. The script will be terminated if the user types 'quit' or 'Quit' or 'QUIT'. The upper() office is used here too 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

impress ( "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

impress ( "The input value is:" , inputVal)

print ( "Enter the text:" )

Output:

The following similar output will appear after executing the in a higher place script. Here, 'Learn python from LinuxHint.com' has given as the first input value and terminated from the script for typing the discussion, 'quit'. Yous have to remember one thing while taking input from the terminal using the fileinput module. That is, y'all have to press ctrl+d subsequently taking the input.

You have to provide the filename every bit the argument value of the fileinput.input() role if you want to have data from the file instead of the concluding.

Decision:

Three different ways to take input from the terminal have been shown in this tutorial by using 3 elementary 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 volition take the standard input based on their requirements later reading this tutorial.

Near the writer

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

quickacess1948.blogspot.com

Source: https://linuxhint.com/read-from-stdin-in-python/

0 Response to "Reads a String From the Standard Input Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel