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.

2
  • Calling the function on a loop? Commented Mar 16, 2014 at 17:24
  • maybe I'm being dense but I can't figure out how to loop over multiple lines of input Commented Mar 16, 2014 at 17:32

2 Answers 2

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)) 
Sign up to request clarification or add additional context in comments.

6 Comments

is there a different input function that will read multiple lines?
No, but you can write your own in the suggested form.
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.
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.
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.
|
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) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.