Back

Explore Courses Blog Tutorials Interview Questions
+3 votes
7 views
in Python by (2.6k points)

I am doing some code golf challenges and the input is in stdin, Can I get the input in Python, If yes then how?

3 Answers

0 votes
by (2k points)
edited by

Just use fileinput module it will loop through all the lines as you command.

to remove trailing newline use liner.strip().

import fileinput

for line in fileinput.input():
    pass

0 votes
by (10.9k points)

You can use the fileinput input module : this module basically implements a helper class and a function to write a loop over standard input or a list of files.

 Syntax-

import fileinput

for line in fileinput.input():

    process(line)

 fileinput loops through all in the input which has been specified as file names in the command-line arguments and if arguments are not provided, it loops through all the standard input.

 

0 votes
by (106k points)
edited by

To read from stdin you can use the below-mentioned code:-

import sys 

for line in sys.stdin:

    print line

You can use the following video tutorials to clear all your doubts:-

Related questions

+3 votes
2 answers
+3 votes
3 answers
+3 votes
2 answers
0 votes
1 answer

Browse Categories

...