Take input from stdin in Python Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we will read How to take input from stdin in Python. There are a number of ways in which we can take input from stdin in Python. sys.stdininput()fileinput.input()Read Input From stdin in Python using sys.stdin First we need to import sys module. sys.stdin can be used to get input from the command line directly. It used is for standard input. It internally calls the input() method. Furthermore, it, also, automatically adds '\n' after each sentence. Example: Taking input using sys.stdin in a for-loop Python3 import sys for line in sys.stdin: if 'q' == line.rstrip(): break print(f'Input : {line}') print("Exit") Output Read Input From stdin in Python using input() The input() can be used to take input from the user while executing the program and also in the middle of the execution. Python3 # this accepts the user's input # and stores in inp inp = input("Type anything") # prints inp print(inp) Output: Read Input From stdin in Python using fileinput.input() If we want to read more than one file at a time, we use fileinput.input(). There are two ways to use fileinput.input(). To use this method, first, we need to import fileinput. Example 1: Reading multiple files by providing file names in fileinput.input() function argument Here, we pass the name of the files as a tuple in the "files" argument. Then we loop over each file to read it. "sample.txt" and "no.txt" are two files present in the same directory as the Python file. Python3 import fileinput with fileinput.input(files=('sample.txt', 'no.txt')) as f: for line in f: print(line) Output: Example 2: Reading multiple files by passing file names from command line using fileinput module Here, we pass the file name as a positional argument in the command line. fileargument parses the argument and reads the file and displays the content of the file. Python3 import fileinput for f in fileinput.input(): print(f) Output: Comment More infoAdvertise with us Next Article Take input from stdin in Python ayushmankumar7 Follow Improve Article Tags : Python python-basics python-input-output Practice Tags : python Similar Reads Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio 10 min read Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is 8 min read Python basic I/O TechniquesTaking input in Python Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. While Python provides us with two inbuilt functions to read the input from the keyboard. input () 3 min read Python input() Function Python input() function is used to take user input. By default, it returns the user input in form of a string.input() Function Syntax: input(prompt)prompt [optional]: any string value to display as input messageEx: input("What is your name? ")Returns: Return a string value as input by the user.By de 4 min read Taking input from console in Python What is Console in Python? Console (also called Shell) is basically a command line interpreter that takes input from the user i.e one command at a time and interprets it. If it is error free then it runs the command and gives required output otherwise shows the error message. A Python Console looks 2 min read Python - Print Output using print() function Python print() function prints the message to the screen or any other standard output device. In this article, we will cover about print() function in Python as well as it's various operations.Python# print() function example print("GeeksforGeeks") a = [1, 2, 'gfg'] print(a) print() Function Syntax 4 min read Python - Output Formatting In Python, output formatting refers to the way data is presented when printed or logged. Proper formatting makes information more understandable and actionable. Python provides several ways to format strings effectively, ranging from old-style formatting to the newer f-string approach.Formatting Out 5 min read Python3 I/ODifferent Input and Output Techniques in Python3 An article describing basic Input and output techniques that we use while coding in python. Input Techniques 1. Taking input using input() function -> this function by default takes string as input. Example: Python3 #For string str = input() # For integers n = int(input()) # For floating or deci 3 min read Like