def ishex(s): hex_digits = set(string.hexdigits) # if s is long, then it is faster to check against a set return all(c in hex_digits for c in s)