how can i do multiple lines of an input field? PYTHON

Viewed 199

python

input(write some text: )

what i want it to output is:

write some text: it was a good day
today as i went to a park

then press enter button twice to go on to the other lines of code

2 Answers
print("enter 'quit' at end of your text")
print("type your text here")
# declare variable to strore string
text = ""
stop_word = "quit"
while True:
    line = input()
    if line.strip() == stop_word:
        break
    # \n is new line, %s is string formatting
    # str.strip() will remove any whitespace that is at start or end
    text += "%s\n" % line.strip()
print(text)

if you want use 3 blank lines as stop word:

print("Please enter 3 blank lines to terminate")
print("type your text here")
# declare variable to strore string
text = ""
stop_word = "quit"
counter = 0
while True:
    line = input()
    if line.strip() == '':
        counter += 1
        if counter == 3:
            break
    # \n is new line, %s is string formatting
    # str.strip() will remove any whitespace that is at start or end
    text += "%s\n" % line.strip()
print(text)

for the result, if you want to get rid of blank lines:

print("Please enter 3 blank lines to terminate")
print("type your text here")
# declare variable to strore string
text = ""
stop_word = "quit"
counter = 0
while True:
    line = input()
    if line.strip() == '':
        counter += 1
        if counter == 3:
            # break terminates while loop
            break
        # continue go to start of while loop
        continue
    # \n is new line, %s is string formatting
    # str.strip() will remove any whitespace that is at start or end
    text += "%s\n" % line.strip()
print(text)

You can use the sys.stdin.readlines to read multiple lines, and break by using ctrl + z (on windows) / ctrl + d.

import sys

msg = sys.stdin.readlines()
Related