In Python, how to check if a string only contains certain characters?
I need to check a string containing only a..z, 0..9, and . (period) and no other character.
Simpler approach? A little more Pythonic?
>>> ok = "0123456789abcdef" >>> all(c in ok for c in "123456abc") True >>> all(c in ok for c in "hello world") False
It certainly isn't the most efficient, but it's sure readable.