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

Jerry Hill malaclypse2 at gmail.com
Mon Jul 23 02:48:19 CEST 2007


On 7/21/07, Alan Gauld <alan.gauld at btinternet.com> wrote:
> > all(char in string.printable for char in testString)
>
> What is all?
> Is that a 2.5 thing (I'm still on 2.4 here)

Yes, it's a 2.5 thing.  All returns true if all of the elements of an
iterable are true.  According to the docs, it is the equivalent of the
following:

     def all(iterable):
         for element in iterable:
             if not element:
                 return False
         return True

Using 'all' with a generator expression has the virtue of only needing
to look at the string until it finds a single element that is false,
and then returning.  Not only that, it's concise (a single line) and
quite readable (at least to me).

-- 
Jerry


More information about the Tutor mailing list