🌐
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
Discussions

python - Pythonically check if a variable name is valid - Stack Overflow
tldr; see the final line; the rest is just preamble. I am developing a test harness, which parses user scripts and generates a Python script which it then runs. The idea is for non-techie folks t... More on stackoverflow.com
🌐 stackoverflow.com
python - Pylint showing invalid variable name in output - Stack Overflow
constants) to be named ALL_UPPERCASE. Therefore it checks whether your variable name matches the regex used for globals, which is: (([A-Z_][A-Z0-9_]*)|(__.*__))$ (note the A-Z ranges). Hence the Invalid name error. More on stackoverflow.com
🌐 stackoverflow.com
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 ... More on stackoverflow.com
🌐 stackoverflow.com
Where can I find a list of which variable names to avoid
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. More on reddit.com
🌐 r/learnpython
20
77
October 16, 2022
🌐
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, ...
🌐
W3Resource
w3resource.com › python-interview › what-are-variables-in-python-rules-for-naming-variables-in-python.php
Python variables: Definition, assignment, and naming rules
August 12, 2023 - Convention: Python is flexible when it comes to naming, but following certain conventions can make your code more readable and consistent. The convention is to use lowercase letters for variable names, with underscores (_) to separate words in multi-word variable names (snake_case). For example, my_variable, student_age, total_amount, item_price. Here are some examples of valid and invalid variable names:
🌐
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:...
🌐
Learn with Yasir
yasirbhutta.github.io › python › docs › variables › variables-basics.html
Python Variables Explained: Basics, Naming Rules & Practical Examples | Learn with Yasir
Learn Python variables with this beginner-friendly guide. Understand variable basics, naming rules (including valid vs invalid names), reserved keywords, and practice with hands-on coding tasks.
🌐
Real Python
realpython.com › python-variables
Variables in Python: Usage and Best Practices – Real Python
January 12, 2025 - These variables follow the rules for creating valid variable names in Python. They also follow best naming practices, which you’ll learn about in the next section. The variable name below doesn’t follow the rules: ... >>> 1099_filed = False File "<input>", line 1 1099_filed = False ^ SyntaxError: invalid decimal literal
🌐
Quora
quora.com › What-are-the-valid-variable-names-in-Python
What are the valid variable names in Python? - Quora
Here you can see that we did not follow the variable rule that’s why we got invalid syntax error. Variable cannot be start with number. I hope you liked my answer. ... RelatedHow can I avoid a Python variable name conflict? Without an explicit declaration, I always wonder whether my new variable name may have been already in use somewhere.
Find elsewhere
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.

🌐
GitHub
github.com › community › community › discussions › 29642
Variables in Python Comment illegal terms below Answers · Discussion #29642 · community/community
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, ...
Author   community
🌐
Dydevops
dydevops.com › tutorials › python › python-variable-names
Python - Variable Names Explained with Examples - Python Tutorial | DyDevOps
May 20, 2025 - Variable names are case-sensitive (name and Name are different). Cannot use Python keywords (e.g., class, if, else, True, etc.). ... 1user = "Invalid" # Starts with a digit user-name = "Error" # Hyphen is not allowed class = "Python" # 'class' is a reserved keyword
🌐
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
Variable names cannot begin with a digit. Now, let’s analyze the given options based on these rules: a. _var - This is valid because it starts with an underscore. b. var_name - This is also valid as it starts with a letter. c. var11 - This is valid since it starts with a letter and includes numbers. d. 5var - This is not valid because it begins with a digit, which violates the naming rule. Overall, the only invalid variable name in this set is 5var.
🌐
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.
🌐
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.
🌐
Aintelligence
aintelligence.in › 2025 › 02 › understanding-variable-naming-in-python.html
Understanding Variable Naming in Python - AIntelligence
February 14, 2025 - # Valid variable names product_price = 99.99 _roll_number = 101 address1 = "New York" # Invalid variable names 1name = "Alice" # Starts with a number customer name = "Bob" # Contains space price$ = 200 # Contains special character if = "condition" # Uses a keyword · By following these rules and best practices, you can create clear, readable, and error-free Python programs.
🌐
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 ...
🌐
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".
🌐
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.
🌐
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 - Variables such as firstname, first_name, and firstname9 adhere to these rules and are therefore valid Python variable names. Answered by TaylorSmith48•49.2K answers•14.6M people helped ... A Primer for Computational Biology - Shawn T. O’Neil · The Missing Link: An Introduction to Web Development and Programming - Michael Mendez · Python for Informatics: Exploring Information - Charles Severance · Upload your school material for a more relevant answer · The invalid variable name in Python from the given options is B.