[Tutor] How to determine if every character in one string is inanother string?

Terry Carroll carroll at tjc.com
Thu Jul 26 00:33:43 CEST 2007


On Tue, 24 Jul 2007, wesley chun wrote:

> i don't have any time myself either (getting ready for OSCON talk),
> but i'm not sure what terry's OP was about... looking for a
> well-written piece of code, a faster-performing snippet, or both?  i
> think he was just unsatissfied with his 1st attempt.

Exactly.  It worked fine, but just seemed unpythonic to me.

To use an analogy, not long ago I thought the best way to see if string X 
contained string Y was:

   if X.find(Y) != -1

Which works just fine.  But another poster pointed out:

   if Y in X:

Which is much more elegant/pythonic; but I didn't know you could do that
with one string over another.  For some reason, I had thought Y would have
to exactly match one iterable element in X (e.g., one element of a list,
or one character of a string) for that to work.

Similarly, I was thinking that while that first attempt of mine worked, 
its apparent-to-me lameness suggested that there was a more idiomatic 
approach, and I wanted to find out what that was.  

Seeing the different approaches put forward by the various contributors
was pretty interesting, though, I must say.




More information about the Tutor mailing list