program1.py:
a = "this is a test"
for x in a:
print(x)
program2.py:
a = """this is a test
with more than one line
three, to be exact"""
for x in a:
print(x)
program3.py:
import sys
for x in sys.stdin:
print(x)
infile.txt:
This is a test
with multiple lines
just the same as the second example
but with more words
Why do program1 and program2 both output each character in the string on a separate line, but if we run cat infile.txt | python3 program3.py
, it outputs the text line by line?
sys.stdin
is not a string - it is special class which was constructed to read line by line. – furas Dec 18 '17 at 3:22sys.stdin
is not astr
object, it is a file-object. – juanpa.arrivillaga Dec 18 '17 at 3:30