Correct variable names consist only of English letters, digits and underscores and they can't start with a digit.
Check if the given string is a correct variable name.
Example
-
- For
name = "var_1__Int"
, the output should bevariableName(name) = true
; - For
name = "qq-q"
, the output should bevariableName(name) = false
; - For
name = "2w2"
, the output should bevariableName(name) = false
.
- For
我的解答:
def variableName(name): dict = {'word':'qwertyuiopasdfghjklzxcvbnm', 'digit':'0123456789', 'underline':'_'} if name[0].lower() in dict['word'] or name[0] in dict['underline']: for i in name: if i.lower() in dict['word'] or i in dict['underline'] or i in dict['digit']: pass else: return False return True else: return False
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
def variableName(name): return name.isidentifier()