I would like to get some reusable code to correctly determine whether or not a string is a valid variable name in python3 (or the currently-running python would suffice).
For example:
If given the string group_찇籸딥햳㸙濮ᚨ麍ڵថ
, the method that solves this problem should return True
, this can be used as a python variable name. If given group_찇籸딥-햳㸙濮ᚨ麍ڵថ
, it should return False
, because it cannot be used correctly as a singular variable name.
>>> group_찇籸딥햳㸙濮ᚨ麍ڵថ = 4
>>> group_찇籸딥-햳㸙濮ᚨ麍ڵថ = 4
File "<stdin>", line 1
SyntaxError: can't assign to operator
I would prefer if the solution avoided unsafe eval
s. A previously-attempted solution was returning True
if the regex ^[\d\W]|[^\w]
was matched, but this seems to be incomplete since it mis-identifies the first example given above as invalid.