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

Error processing SSI file


  1. Branson

    • 2021/4/9

    *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()

    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.

  2. Kayson

    • 2016/1/17

    Python Multiline String using triple quotes. If your long string has newline characters, then you can use triple quotes to write them into multiple lines. Note that 

  3. Leroy

    • 2019/10/30

    Note that in Python 3, we use input() in place of raw_input(). This article is contributed by Abhishek Shukla . Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

  4. Edwards

    • 2018/12/12

    In Python, we can read a line of input from stdin using the following syntax: Python 2 syntax. # read a line from STDIN my_string = raw_input(). Python 3 syntax.

  5. Marcus

    • 2020/6/13

    Try this

    import sys
    lines =






    OUTPUT: ['1', '2', '3', '4']

  6. Lewis

    • 2018/3/30

    One way to make multi-line literal strings look good is to use a backslash to escape the newline, like this: s = '''\ ********************************************************************* hello ********************************************************************* ''' print(s)

  7. Boone

    • 2019/12/14

    What about using .splitlines() ? for line in textData.splitlines(): print(line) lineResult = libLAPFF.parseLine(line).

  8. Everett

    • 2015/11/3

    Otherwise, it's easy to make a generator/function to spit out three lines at a time, which you can then throw into a function that parses the three lines. The '3-lines-at-a-time' part of your problem is the easy part. def return_3(file): return [ for i in range(3)]

  9. Santana

    • 2020/4/27

    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:
        text += "%s\n" % line
    print text
  10. Garcia

    • 2017/2/10

    How does input work in Python 3?

  11. Caiden

    • 2020/9/6

    The problem is, I can't figure out how to read the multiple line input test cases for my first program If you get the input from some source all at once, you can use

  12. Gianni

    • 2020/9/11

    Taking multiple inputs from user in Python. Developer often wants a user to enter multiple values or inputs in one line. In C++/C user can take multiple inputs in one line using scanf but in Python user can take multiple values or inputs in one line by two methods. Using split () method. Using List comprehension.

  13. Jacoby

    • 2021/9/15
    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))
  14. Maximo

    • 2016/8/6

    There are multiple ways to do this : Method 1: Using triple Quotes [code]string='''​This is multi line string''' [/code]Output : [code]string 'This \nis multi line\nstring' 

  15. Alistair

    • 2019/5/20

    Split by line break: splitlines() There is also a splitlines() for splitting by line boundaries.. str.splitlines() — Python 3.7.3 documentation; As in the previous examples, split() and rsplit() split by default with whitespaces including line break, and you can also specify line break with the parmeter sep.

  16. Giuliani

    • 2017/5/25


  17. Prishtina

    • 2020/10/16

    Alternatively,You can try

    import sys
    s =
  18. Myers

    • 2020/11/24

    Could somebody please explain to me in layman's terms, what the newline = " " argument does It's sufficient for now to just accept that there is good reason for the [input] with open('./data/competitions-from-python.csv', 'w', newline='') as f: 

  19. Sala

    • 2017/10/27

    Returns true if the last line was read from sys.stdin, otherwise returns false. fileinput.nextfile ¶ Close the current file so that the next iteration will read the first line from the next file (if any); lines not read from the file will not count towards the cumulative line count. The filename is not changed until after the first line of the

  20. Valentini

    • 2019/5/18

    Let's say you have a file with 1 billion records and asked to parse the file to read and process the data from it. What does an R mean before a string in Python?

  21. Shehu

    • 2017/10/23 can be used to take multiline input from user. For example

    >>> import sys
    >>> data =
      line one
      line two
      line three
    >>> for line in data.split(sep='\n'):
    o/p:line one
        line two
        line three
  22. Carson

    • 2015/3/2

    Multi-line Statement in Python. Usually, every Python statement ends with a newline character. However, we can extend it over to multiple lines using the line continuation character (\). And Python gives us two ways to enable multi-line statements in a program. Explicit line continuation

  23. Jax

    • 2017/4/7

    You can repeatedly call input() in a loop until it receives a blank line. You can repeatedly call input() in a loop until the user does ctrl-D on a UNIX-like OS, at which point EOFError will be raised which you can catch. Read the data from a text file or other more appropriate source than stdin.

  24. Elisha

    • 2020/12/22

    One solution would be a list comprehension and the slice operator: with open(​filename, 'r') as infile: lines = [line for line in infile][:N]. After this 

  25. Banks

    • 2016/6/1

    Just extending this answer instead of any stop word you can just check whether a line is there or not

    content = []
    while True:
        line = raw_input()
        if line:

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

    print '\n'.join(content)
  26. Matias

    • 2020/8/16

    Introduced in Python 3.4, pathlib has a really convenient method for reading in text from files, as follows: from pathlib import Path p = Path('my_text_file') lines = p.read_text().splitlines() (The splitlines call is what turns it from a string containing the whole contents of the file to a list of lines in the file).

  27. Zayne

    • 2020/3/16

    #taking multiple integer inputs on a single line; l = map(int,input().split()) Is there a way to read only a single integer from a line of integers in Python 3.x?

  28. Fletcher

    • 2015/3/3

    User Input The input Function The input() function is used to get data from the user in Python Command Line programs. Syntax <return> = input( "<prompt>") Where prompt is a string that will be display for the user to view which should provide some kind of indication of the usage and type of data you are expecting and return is the returned value from the function.

  29. River

    • 2021/6/1

    To read multiple variables in language C, we write something like −//Read three variable in one line scanf(“%d %d %d”, &x, &y, &z)Currently 

  30. Jason

    • 2020/5/27

    Most programs today use a dialog box as a way of asking the user to provide some type of input. While Python provides us with two inbuilt functions to read the input from the keyboard. raw_input ( prompt ) input ( prompt ) raw_input ( ) : This function works in older version (like Python 2.x). This function takes exactly what is typed from the keyboard, convert it to string and then return it to the variable in which we want to store.

  31. Finn

    • 2017/10/7

    What if you can get user input in multiple lines, just like one field per line. Let's do that! Taking user input for the multiple lines is not difficult in Python. And here is a​ 

  32. Patel

    • 2020/10/17

    Multi-line string syntax Another way is to use the built-in multi-line string syntax, like so: template = “””This is the first line.<br /><br /><br /> This is the second line.<br /><br /><br /> This is the third line.””” 1

  33. De Angelis

    • 2016/1/23


  34. Kamari

    • 2016/1/24

    How do you input multiple inputs in Python?

  35. Eliseo

    • 2015/9/12

    Python supports multi-line continuation inside parentheses ( ), brackets [ ], and braces { }. The brackets are used by List and the braces are used by dictionary objects. We can use parentheses for expressions, tuples, and strings.

  36. Lombardi

    • 2021/3/31

    To take multiple lines of input and put them in a list of strings, you can use sys.​stdin.readlines() which will keep accepting returns until you give it an EOF [ctrl + d 

  37. Brett

    • 2018/3/27

    This is also the only way to write “real” comment blocks in Python that are ignored by the parser. If it bothers you that Python doesn't support proper multiline 

  38. Thiago

    • 2021/10/4

    Python 3 This is a tutorial in Python3, but this chapter of our course is available in a version for Python 2.x as well: Keyboard Input in Python 2.x. Classroom Training Courses The goal of this website is to provide educational material, allowing you to learn Python on your own.

  39. Rome

    • 2020/2/10

    In Python 3.x the raw_input() of Python 2.x has been replaced by input() function. However in both the cases you cannot input multi-line strings, 

  40. Rocco

    • 2018/10/19

    Reading multiple lines of input So I'm trying to get into regular programming practice. Spent some time doing challenges at CodeEval and the likes with C++ a little ways back, but now that I'm coming back I want to focus a little on Python.

  41. Gallo

    • 2017/3/22

    In C++/C user can take multiple inputs in one line using scanf but in Python In Python 3.x the raw_input() of Python 2.x has been replaced by input() function.

  42. Daniel

    • 2016/6/10

    For most of the purposes, the newline character can be used to specify a new line. You can put this character within Python Strings. An example of using character in a string to display output to the console screen is given below. name = "Gurmeet Singh"

Comments are closed.

More Posts