How do I read multiple lines of raw input in Python?

Posted on

Solving problem is about exposing yourself to as many situations as possible like How do I read multiple lines of raw input in Python? and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about How do I read multiple lines of raw input in Python?, which can be followed any time. Take easy to follow this discuss.

How do I read multiple lines of raw input in Python?

I want to create a Python program which takes in multiple lines of user input. For example:

This is a multilined input.
It has multiple sentences.
Each sentence is on a newline.

How can I take in multiple lines of raw input?

Asked By: felix001

||

Answer #1:

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))
Answered By: jamylak

Answer #2:

Alternatively, you can try sys.stdin.read() that returns the whole input until EOF:

import sys
s = sys.stdin.read()
print(s)
Answered By: Venkat

Answer #3:

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 += "%sn" % line
print text
Answered By: Junuxx

Answer #4:

Just extending this answer https://stackoverflow.com/a/11664652/4476612
instead of any stop word you can just check whether a line is there or not

content = []
while True:
    line = raw_input()
    if line:
        content.append(line)
    else:
        break

you will get the lines in a list and then join with n to get in your format.

print 'n'.join(content)
Answered By: Rushikesh Gaidhani

Answer #5:

*I struggled with this question myself for such a long time, because I wanted to find a way to read multiple lines of user input without the user having to terminate it with Control D (or a stop word).
In the end i found a way in Python3, using the pyperclip module (which you’ll have to install using pip install)
Following is an example that takes a list of IPs
*

import pyperclip
lines = 0
while True:
    lines = lines + 1 #counts iterations of the while loop.
    text = pyperclip.paste()
    linecount = text.count('n')+1 #counts lines in clipboard content.
    if lines <= linecount: # aslong as the while loop hasn't iterated as many times as there are lines in the clipboard.
        ipaddress = input()
        print(ipaddress)
    else:
        break

For me this does exactly what I was looking for; take multiple lines of input, do the actions that are needed (here a simple print) and then break the loop when the last line was handled. Hope it can be equally helpful to you too.

Answered By: Jon

Answer #6:

Try this

import sys
lines = sys.stdin.read().splitlines()
print(lines)

INPUT:

1

2

3

4

OUTPUT:
[‘1’, ‘2’, ‘3’, ‘4’]

Answered By: Arpitt Desai

Answer #7:

The easiest way to read multiple lines from a prompt/console when you know exact number of lines you want your python to read, is list comprehension.

lists = [ input() for i in range(2)]

The code above reads 2 lines. And save inputs in a list.

Answered By: KailiC

Answer #8:

sys.stdin.read() can be used to take multiline input from user. For example

>>> import sys
>>> data = sys.stdin.read()
  line one
  line two
  line three
  <<Ctrl+d>>
>>> for line in data.split(sep='n'):
  print(line)
o/p:line one
    line two
    line three
Answered By: vishnu vardhan

Leave a Reply

Your email address will not be published.