3

With this code:

A = raw_input("Please input text here")
print A
print len(A)

For the input, when I copy and paste from notepad, it only recognizes the first line of the following:

Cars are fast
Motorcycles are faster
Planes are even faster

Therefore, print A will print 'Cars are fast' and print len(A) with print 13, which is the length of 'Cars are fast'.

How can I get Python to recognize the remaining lines of my input?

Thank you.

anon582847382
  • 19,907
  • 5
  • 54
  • 57
user3422147
  • 71
  • 1
  • 4

2 Answers2

5

raw_input will read one line of input only.

See answers to this question for a general overview of getting input from the command line.

If you want to use raw_input then a loop will read all your lines but you will need a way to break out, something like this.

while True:
    A = raw_input("Please input text here (Q to quit)")
    if len(A) == 1 and A[0].lower() == "q":
        break
    print A
    print len(A)

To collate multiple lines do something like this

data = []
while True:
    A = raw_input("Please input text here (Q to quit)")
    if len(A) == 1 and A[0].lower() == "q":
        break
    data.append(A)

print data
for A in data:
    print len(A)

Remember to enter a newline after pasting. Also, the raw_input prompt messages may not display correctly.

You could go crazy and manage the prompt. Expecting a zero length input means the user is trying to quit.

data = []
prompt = "Please input text (Q to quit):\n"
while True:
    if data:
        A = raw_input()
    else:
        A = raw_input(prompt)    
    while len(A) == 0:
        A = raw_input(prompt)
    if len(A) == 1 and A[0].lower() == "q":
        break        
    data.append(A)

for A in data:
    print "%s - %i" % (A, len(A))
Community
  • 1
  • 1
Graeme Stuart
  • 5,837
  • 2
  • 26
  • 46
  • is there a different input function that will read multiple lines? – user3422147 Mar 16 '14 at 17:33
  • No, but you can write your own in the suggested form. – Graeme Stuart Mar 16 '14 at 17:35
  • thanks for taking the time to reply. I might be wrong but the way that you're suggesting I do it would require me to manually enter each line of text wouldn't it? What I would like to do is input any number of lines, and then have python return a list of strings so that list[0] would be the first line, list[1] would be the second line, etc. – user3422147 Mar 16 '14 at 17:45
  • You can copy and paste multiple lines at once and the programme will read each line in turn. You might want to collect all the data into a list before processing it. – Graeme Stuart Mar 16 '14 at 17:47
  • A small deviation from this could be using '' (a blank string) as a sentinel instead of 'q', it all depends upon your requirement. But AFAIK Python doesn't have a inbuilt multi line read. – Jagjeet Singh Thukral Mar 16 '14 at 18:05
  • Of course @JagjeetSinghThukral is correct, the 'Q' is optional and depends on what you need. – Graeme Stuart Mar 16 '14 at 18:11
0

There is sys.stdin object that has read method. By default method reads till end of file.

>>> import sys
>>> file_content = sys.stdin.read() # press CTRL+D to close stdin

You can read more about this method using:

>>> help(sys.stdin.read)

Or

>>> help(sys.read)
aisbaa
  • 9,867
  • 6
  • 33
  • 48