Maximum characters that can be stuffed into raw_input() in Python

Question:

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

Asked By: Srini Kadamati

||

Answers:

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

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.

Answered By: David Vincent

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.

Answered By: Eric F.
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.