Monday, June 20, 2016

Reading lines till eof from standard input

If you want to read lines from the standard input until End-Of-File (eof), you can use the sys.stdin, for example:
#!/usr/bin/env python3
import sys

for line in sys.stdin:
    print(line,end="",flush=True)

Printing without newline

Print automatically adds a new line after printing its input variables and/or constants. To avoid this new line in Python2.6+ you can add a colon "," (without quotes) after the printing command, here is an example
print 'Hello',
print 'World'
In Python3, you can add the option end="". Example:
print('Hello',end="")
print('World')
Remember that from Python2.6+ you also can use the Python3 print command, adding
from __future__ import print_function