[Tutor] How to determine if every character in one string is in another string?
Luke Paireepinart
rabidpoobear at gmail.com
Sat Jul 21 04:21:17 CEST 2007
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] == []
>
Well, my first thought was (assuming s is your string:)
def printable(s):
import string
return s.strip(string.printable) == ""
> But that just seems lame.
>
This seems kinda like an abuse of strip, but it should work (since your
condition was that all characters must be printable;
a different condition may not be satisfied by the strip because it only
removes characters from the ends.)
Let me know if this works :)
-Luke
More information about the Tutor
mailing list