Extract all words that begin with x

Terry Reedy tjreedy at udel.edu
Wed May 12 12:32:38 EDT 2010


On 5/12/2010 11:33 AM, Aahz wrote:

>>> also, what if the OP intended "words that begin with x" with x a string
>>> (as opposed to a single character) ?
>>
>>      word[:len(x)] == x
>>
>> will work in that case.
>
> But that's now going to be slower.  ;-)  (Unless one makes the obvious
> optimization to hoist len(x) out of the loop.)

If x were a known literal, then len(x) could be replaced by the actual 
size. But I admit that that is a nuisance and point of failure. I think 
comparing start/end strings of variable length or length greater than 
one is the real justification for the new methods, not the OP's trivial 
single-char case.

Terry Jan Reedy






More information about the Python-list mailing list