The string I'm testing can be matched with
[\w-]+. Can I test if a string conforms to this in Python, instead of having a list of the disallowed characters and testing for that?
If you want to test a string against a regular expression, use the re library
import re valid = re.match('^[\w-]+$', str) is not None
Python has regex as well:
import re if re.match('^[\w-]+$', s): ...
Or you could create a list of allowed characters:
from string import ascii_letters if all(c in ascii_letters+'-' for c in s): ...