I know there is one method
print() for input and output in python, but sometimes, it create time limit exceeded error on the online platforms such as Hackerearth or other online judge. I want to know what are different method for input and output in python other than this?
There are some of the specific functions that can be used for fast input and output.
Python provides two file objects “stdin” and “stdout” while are a part of “sys” module, we can use readline() method of “stdin” for input and write() function of “stdout” for output.
It is used to take the input, it takes the input as a string by default and if you want to take input as an integer then use the eval() or int() functions. If you want to take space separated inputs in a single line then use split().
instead of eval() you can also use int(). If you do not use any of these functions, input will be string by default
It gives the output in string type. And if you want the multiple outputs in a single statement, use ‘+’ in the between the two.
stdout.write('p', + ,'A', +, 'C')
stdin.readline() # for input stdout.write() # for output
To use the fast input output functions, you need to import the files which contain them i.e. “sys module”.
for Example :
n = int(input()) a,b = map(int,input().split()) print(n)
can be written as
n = int(stdin.readline()) # input using fast i/p method a,b = map(int,input().split()) stdout.write(str(n)) #input using fast o/p method