Terry Carroll wrote:
> Is there a straightforward way to find out if all characters in one string 
> are present in a second string?
> 
> Basically, I have a string s, and I want to print it out only if every
> character in it is printable (because I accidentally brought my PC loudly
> to its knees printing a few thousand BEL characters when trying to debug
> something).  A workable test for me is whether every character in the
> string to be printed is in string.printable.
> 
> Right now I'm doing:
> 
> def printable(s):
>     import string
>     return [x for x in s if x not in string.printable] == []

My first thought is to use sets (not tested):

_printable = set(string.printable)
def printable(s):
   return set(s).issubset(printable)

Using string.translate() might be very fast. Something like

all_chars = ''.join(chr(i) for i in range(255))
def printable(s):
   return not s.translate(all_chars, string.printable)

Kent
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to