3

For an InterviewStreet challenge, we have to be able to accomodate for a 10,000 character String input from the keyboard, but when I copy/paste a 10k long word into my local testing, it cuts off at a thousand or so.

What's the official limit in Python? And is there a way to change this?

Thanks guys

Here's the challenge by-the-by:

http://www.interviewstreet.com/recruit/challenges/solve/view/4e1491425cf10/4edb8abd7cacd

2
  • there is no such limit. there might be a stray newline in your pasted input. Commented Dec 12, 2011 at 2:10
  • Dupe of stackoverflow.com/questions/7357007/… ? Commented Dec 12, 2011 at 2:17

3 Answers 3

8

Are you sure of the fact that your 10k long word doesn't contain newlines?


raw_input([prompt])

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised.

...

If the readline module was loaded, then raw_input() will use it to provide elaborate line editing and history features.

There is no maximum limit (in python) of the buffer returned by raw_input, and as I tested some big length of input to stdin I could not reproduce your result. I tried to search the web for information regarding this but came up with nothing that would help me answer your question.

my tests

  :/tmp% python -c 'print "A"*1000000' | python -c 'print len (raw_input ())';
  1000000
  :/tmp% python -c 'print "A"*210012300' | python -c 'print len (raw_input ())';
  210012300
  :/tmp% python -c 'print "A"*100+"\n"+"B"*100' | python -c 'print len (raw_input ())'; 
  100
Sign up to request clarification or add additional context in comments.

1 Comment

+1 Thanks for the excellent, simple demonstration. Nice work.
6

I had this same experience, and found python limits the length of input to raw_input if you do not import the readline module. Once I imported the readline module, it lifted the limit (or at least raised it significantly enough to where the text I was using worked just fine). This was on my Mac with Python 2.7.15. Additionally, it’s been confirmed working on at least 3.9.5.

3 Comments

Wow, thanks a million! This worked for me as well (Python 2.7.17, Ubuntu 18.04).
Worked for me on Python 3.9.5, macOS.
Thanks @Dae—I updated the answer to note that.
0

I guess this is part of the challenges. The faq suggest raw_input() might not be the optimal approach:

The most common (possibly naive) methods are listed below. (...)

There are indeed Python standard modules helping to handle system input/output.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.