Others have linked the official list of built-in functions https://docs.python.org/3/library/functions.html - these are all valid variable names but you should not use them to avoid confusion. There is also an official list of keywords https://docs.python.org/3/reference/lexical_analysis.html#keywords - these are not valid variable names and cannot be used except in their intended context. The exception to that are match and case which are "soft keywords" for backwards compatibility reasons. Moving forward it's advised not to use those as variables, but you might encounter them when reading others' code. There's also some nuance with _, __, etc. but by convention you should only use those in special cases. There's more info about those in the link above. Answer from TangibleLight on reddit.com
🌐
W3Schools
w3schools.com › python › python_variables_names.asp
Python - Variable Names
A variable name cannot be any of the Python keywords. ... myvar = "John" my_var = "John" _my_var = "John" myVar = "John" MYVAR = "John" myvar2 = "John" Try it Yourself » ... Variable names with more than one word can be difficult to read. There are several techniques you can use to make them more readable: Each word, except the first, starts with a capital letter: ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
🌐
Log2Base2
log2base2.com › programming-language › python3 › basic › need-of-variables-in-python.html
Valid and invalid variable names in Python | Need of variables
1. Variable name should start with letter(a-zA-Z) or underscore (_). ... pass, break, continue.. etc are reserved for special meaning in Python.
Discussions

python - Pylint showing invalid variable name in output - Stack Overflow
Now, my question is why pylint is showing my variable names as Invalid name. Is naming variable this way a wrong coding convention. My complete pylint output. ... As your code is not contained in a class or function it is expecting those variables to be constants and as such they should be ... More on stackoverflow.com
🌐 stackoverflow.com
python - Pythonically check if a variable name is valid - Stack Overflow
Notably, TypeError is consistently ... with an invalid key, so this will work correctly on anything you pass to it. ... **kwargs can contain non-valid variable names. E.g. is_valid_variable_name('[]') returned True. I was not able to find any string, where this function returns False. Might be different in python ... More on stackoverflow.com
🌐 stackoverflow.com
validation - How do I convert a string to a valid variable name in Python? - Stack Overflow
I need to convert an arbitrary string to a string that is a valid variable name in Python. Here's a very basic example: s1 = 'name/with/slashes' s2 = 'name ' def clean(s): s = s.replace('/', '... More on stackoverflow.com
🌐 stackoverflow.com
Website to check Illegal variable names or keywords Python - Stack Overflow
I may have stumbled on an illegal variable name pass = "Pass the monkey!" print pass Syntax error: invalid syntax I'm aware that some keywords are verboten as variables. Is there the Pythonic More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/learnpython › where can i find a list of which variable names to avoid
r/learnpython on Reddit: Where can I find a list of which variable names to avoid
October 16, 2022 -

I found lists of "reserved keywords" for python, but there seems to be other words which python lets you use for variable names, but maybe it's better not to use these names?

Such as 'file' or 'sum,' etc

Is there a complete list of such words?

🌐
Apmonitor
apmonitor.com › che263 › index.php › Main › PythonBasics
Python Programming Basics
Valid variable names are those ... __myVariable MYVARIABLE myvariable · Invalid variable names include those that start with a number, have a space in the name, or contain special characters such as:...
🌐
Quora
quora.com › What-variable-names-are-not-allowed-in-Python
What variable names are not allowed in Python? - Quora
Answer (1 of 5): So, there are a few rules that Python Variable names follow. * They cannot start with a number * They can’t contain white space or certain logical/arithmetic operators (think “=” or “&” etc) * And they cannot contain keywords in python.
Top answer
1 of 6
65

In Python 3 you can use str.isidentifier() to test whether a given string is a valid Python identifier/name.

>>> 'X'.isidentifier()
True
>>> 'X123'.isidentifier()
True
>>> '2'.isidentifier()
False
>>> 'while'.isidentifier()
True

The last example shows that you should also check whether the variable name clashes with a Python keyword:

>>> from keyword import iskeyword
>>> iskeyword('X')
False
>>> iskeyword('while')
True

So you could put that together in a function:

from keyword import iskeyword

def is_valid_variable_name(name):
    return name.isidentifier() and not iskeyword(name)

Another option, which works in Python 2 and 3, is to use the ast module:

from ast import parse

def is_valid_variable_name(name):
    try:
        parse('{} = None'.format(name))
        return True
    except SyntaxError, ValueError, TypeError:
        return False

>>> is_valid_variable_name('X')
True
>>> is_valid_variable_name('123')
False
>>> is_valid_variable_name('for')
False
>>> is_valid_variable_name('')
False
>>> is_valid_variable_name(42)
False

This will parse the assignment statement without actually executing it. It will pick up invalid identifiers as well as attempts to assign to a keyword. In the above code None is an arbitrary value to assign to the given name - it could be any valid expression for the RHS.

2 of 6
3

EDIT: this is wrong and implementation dependent - see comments.

Just have Python do its own check by making a dictionary with the variable holding the name as the key and splatting it as keyword arguments:

def _dummy_function(**kwargs):
    pass

def is_valid_variable_name(name):
    try:
        _dummy_function(**{name: None})
        return True
    except TypeError:
        return False

Notably, TypeError is consistently raised whenever a dict splats into keyword arguments but has a key which isn't a valid function argument, and whenever a dict literal is being constructed with an invalid key, so this will work correctly on anything you pass to it.

Find elsewhere
🌐
GitHub
github.com › orgs › community › discussions › 29642
Variables in Python Comment illegal terms below Answers · community · Discussion #29642
Built-in Python keywords are not allowed to be used as variable names since they have other meanings (see https://www.w3schools.com/python/python_ref_keywords.asp), also note that all terms and keywords are case-sensitive. eg. del, yield, return, ...
🌐
Runestone Academy
runestone.academy › ns › books › published › py4e-int › variables › variable-names-keywords.html
2.3. Variable names and keywords — Python for Everybody - Interactive
The variable name 76trombones is illegal because it begins with a number. The name more@ is illegal because it contains an illegal character, @. But what’s wrong with class? It turns out that class is one of Python’s keywords. The interpreter uses keywords to recognize the structure of the program, and they cannot be used as variable names.
🌐
Brainly
brainly.com › computers and technology › high school › which of the following is not a valid variable in python? a. _var b. var_name c. var11 d. 5var
[FREE] Which of the following is not a valid variable in Python? a. _var b. var_name c. var11 d. 5var - brainly.com
Examples of valid variable names include '_var', 'var_name', and 'var11', while '5var' is an invalid name because it starts with a number. Python's official documentation outlines the rules for valid variable naming, which clearly states that ...
🌐
MAKE ME ANALYST
makemeanalyst.com › home › python programming › python variable names and keywords
Python Variable Names and Keywords - MAKE ME ANALYST
December 10, 2017 - So, variable1 is valid while 1variable is a invalid name. You may use uppercase letters for variable names but it is always perfectly fine to begin variable names with a lowercase letter. If your Variable name is long, then you can use underscore character (_) in the name. For example, top_five_members, var_1 etc. all are valid example. You can’t use special characters like !, @, #, $, % etc. in variable name. Python keywords cannot be used as variable name.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-variables
Python Variables - GeeksforGeeks
Below listed variables names are invalid: Python · 1name = "Error" # Starts with a digit class = 10 # 'class' is a reserved keyword user-name = "Doe" # Contains a hyphen · Basic Assignment: Variables in Python are assigned values using the = operator. Python · x = 5 y = 3.14 z = "Hi" Dynamic Typing: Python variables are dynamically typed, meaning the same variable can hold different types of values during execution.
Published   2 weeks ago
🌐
Quora
quora.com › What-are-the-valid-variable-names-in-Python
What are the valid variable names in Python? - Quora
Examples of invalid identifiers: 1var (starts with digit), my-var (hyphen), class (reserved keyword) Reserved keywords (cannot be used as variable names) — Python 3.12+ (common list; check your interpreter for the exact set up to May 2024) False, None, True, and, as, assert, async, await, break, class, continue, def, del, elif, else, except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield
🌐
Stack Overflow
stackoverflow.com › questions › 45685867 › how-to-use-invalid-variable-names-in-exec-or-eval
python - How to use invalid variable names in exec or eval? - Stack Overflow
You would have to map each key in d to a valid Python variable name such as _0, _1, etc. Then use those names in the code that's executed. You haven't said where that code comes from, so it may not be feasible. ... How about this? I confirmed this works correctly. But this can not handle new invalid variable allocation like ":b = :a + 1".
🌐
Sanfoundry
sanfoundry.com › python-questions-answers-variable-names
Variable Names - Python Questions and Answers - Sanfoundry
December 30, 2025 - Answer: b Explanation: Variable names in Python cannot start with a digit. Since 1st_string begins with the digit 1, it is invalid.
🌐
Brainly
brainly.com › computers and technology › high school › which of the following is an invalid variable name in python? a. firstname b. first #name c. first_name d. firstname9
[FREE] Which of the following is an invalid variable name in Python? A. firstname B. first #name C. first_name - brainly.com
March 21, 2024 - Thus, the correct identification of option B as an invalid variable name is crucial for writing effective Python code. Examples of valid variable names include 'my_variable', 'variable1', and '_hiddenVariable'. Invalid examples would be '2ndVariable' (starts with a number) or 'price#tag' (contains a special character).
🌐
Pluralsight
pluralsight.com › tech insights & how-to guides › tech guides & tutorials
Python Variables and Assignment | Pluralsight
September 26, 2018 - An initial character which is not an underscore or a letter from A-Z or a-z will produce an error. The backtick (`) character for example: >>> `ticked = 1 File "<stdin>", line 1 `ticked = 1 ^ SyntaxError: invalid ...