Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading unknown number of lines from console

I have to read unknown number of lines from stdin in python3. Is there a way to do this without any modules? One more question: How to denote end of input for multiple lines in python3?

like image 495
Tojra Avatar asked Oct 26 '25 17:10

Tojra


1 Answers

Try something like this

a_lst = []          # Start with empty list
while True:
    a_str = input('Enter item (empty str to exit): ')
    if not a_str:   # Exit on empty string.
        break
    a_lst.append(a_str)
print(a_lst)
like image 139
Devesh Kumar Singh Avatar answered Oct 29 '25 06:10

Devesh Kumar Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!