Well on top of the regular expressions mentioned you need to make sure it is not one of the reserved keywords :
and del from not while
as elif global or with
assert else if pass yield
break except import print
class exec in raise
continue finally is return
def for lambda try
So something like this :
reserved = ["and", "del", "from", "not", "while", "as", "elif", "global", "or", "with", "assert", "else", "if", "pass", "yield", "break", "except", "import", "print", "class", "exec", "in", "raise", "continue", "finally", "is", "return", "def", "for", "lambda", "try"]
def is_valid(keyword):
return (keyword not in reserved and
re.match(r"^(?!\d)\w+$", keyword) # from p.s.w.g answer
Or like @nofinator suggests you can and should probably just use keyword.iskeyword().
^at the front so the letter (no number) occurs at the beginning...