How do I read multiple lines of raw input in Python?
sentinel = '' # ends when this string is seen
for line in iter(raw_input, sentinel):
pass # do things here
To get every line as a string you can do:
'\n'.join(iter(raw_input, sentinel))
Python 3:
'\n'.join(iter(input, sentinel))
Alternatively, you can try sys.stdin.read()
that returns the whole input until EOF
:
import sys
s = sys.stdin.read()
print(s)
Keep reading lines until the user enters an empty line (or change stopword
to something else)
text = ""
stopword = ""
while True:
line = raw_input()
if line.strip() == stopword:
break
text += "%s\n" % line
print text