Identifiers in Python: Rules, Examples, and Best Practices

When I first started working with Python over a decade ago, one of the earliest lessons I learned was the importance of naming things properly. Whether it’s a variable, a function, or a class, the names we choose, known as identifiers in Python, play a big role in how readable and maintainable our code becomes.

In this tutorial, I’ll walk you through what identifiers are, the rules that govern them, and a few best practices I’ve learned over the years.
I’ll also show you practical Python examples so you can understand how to use identifiers correctly in your own code.

What Is an Identifier in Python?

An identifier in Python is simply the name you give to a variable, function, class, module, or any other object. Think of it as a label that helps Python (and you) identify that particular piece of data or code.

For example, when you create a variable to store the number of employees in your company, the variable name itself is the identifier.

# Example of a simple identifier in Python
num_employees = 120

Here, num_employees is the identifier. It points to the value 120. This name helps you refer to this piece of data later in your program.

Rules for Writing Identifiers in Python

Python has a few simple but strict rules for naming identifiers. If you break any of these, your code won’t run.

Here are the official rules you must follow:

  1. Identifiers can contain letters (A–Z, a–z), digits (0–9), and underscores (_).
  2. An identifier cannot start with a digit.
  3. They are case-sensitive. (Name and name are two different identifiers.)
  4. You cannot use Python keywords as identifiers.
  5. Special characters like @, #, $, %, and spaces are not allowed.

Let’s look at a few examples.

# Valid identifiers in Python
employee_name = "John"
total_sales_2025 = 45900
_state = "California"
AverageSalary = 85000

# Invalid identifiers in Python
2nd_employee = "Sarah"     # Starts with a digit
employee-name = "Tom"      # Contains a hyphen
class = "Manager"          # Uses a reserved keyword

Each invalid identifier above will cause a syntax error in Python. Always remember, Python identifiers must follow these basic rules to work properly.

Python Keywords You Cannot Use as Identifiers

Python has a list of reserved words (called keywords) that have special meanings. You cannot use these as identifiers because Python uses them to define its syntax and structure.

Here’s how you can see all the keywords in your current Python version:

# Get a list of all Python keywords
import keyword
print(keyword.kwlist)

When you run this code, you’ll see a list like:

['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', ...]

You can see the output in the screenshot below.

isidentifier

These words are reserved. If you try to use any of them as variable names, Python will throw an error.

Case Sensitivity in Python Identifiers

One important thing you must remember is that Python identifiers are case-sensitive. That means Name, name, and NAME are all treated as different identifiers.

# Case sensitivity example
Name = "Alice"
name = "Bob"
NAME = "Charlie"

print(Name)   # Output: Alice
print(name)   # Output: Bob
print(NAME)   # Output: Charlie

You can see the output in the screenshot below.

python identifier

Even though these look similar, Python treats them as separate variables. So be consistent with your naming style to avoid confusion.

Naming Conventions for Identifiers in Python

While the rules above are mandatory, Python also has naming conventions, guidelines that make your code easier to read and maintain.
These conventions are part of the PEP 8 style guide, which is the official style guide for Python.

Here are some common naming conventions I follow:

TypeConventionExample
VariableLowercase with underscorestotal_sales
FunctionLowercase with underscorescalculate_tax()
ClassCapitalized words (PascalCase)EmployeeRecord
ConstantAll uppercase with underscoresTAX_RATE = 0.07
Private variablePrefix with underscore_salary

Following these conventions helps your Python code look professional and consistent.
It also makes it easier for other developers to understand your logic.

Method 1 – Use Descriptive Identifiers in Python

When naming identifiers, always be descriptive. A descriptive name tells you exactly what the variable or function does.

Here’s an example from one of my real-world Python projects:

# Descriptive identifiers in Python
def calculate_annual_bonus(employee_salary, performance_rating):
    if performance_rating == "Excellent":
        return employee_salary * 0.20
    elif performance_rating == "Good":
        return employee_salary * 0.10
    else:
        return employee_salary * 0.05

bonus = calculate_annual_bonus(80000, "Excellent")
print("Annual Bonus:", bonus)

You can see the output in the screenshot below.

what is identifier in python

In this example, the identifiers employee_salary, performance_rating, and calculate_annual_bonus clearly describe their purpose. This makes the code self-explanatory without needing extra comments.

Method 2 – Avoid Ambiguous or Misleading Identifiers

Another best practice is to avoid ambiguous or misleading names. Names like data, info, or temp don’t tell you much about what they store.

Here’s a bad example:

# Not recommended
data = 120000

Now compare that with a better version:

# Better identifier
annual_revenue_usd = 120000

The second version instantly tells you what the variable represents. Small naming improvements like this can make your Python code much easier to maintain.

Method 3 – Use Underscores Effectively in Python Identifiers

Python allows underscores in identifiers, and when used wisely, they make names more readable. There are different ways to use underscores depending on the context.

# Using underscores in Python identifiers
first_name = "Emily"
last_name = "Clark"
_full_name = first_name + " " + last_name  # Private variable

Underscores help separate words and make identifiers easier to read. They also indicate scope or purpose (like _variable for private use).

Method 4 – Use Built-in Function Names as Identifiers (and Why You Shouldn’t)

Python comes with many built-in functions such as sum, max, list, and input. If you use these names as identifiers, you’ll overwrite the built-in function, which can cause unexpected behavior.

# Avoid overwriting built-in Python identifiers
sum = 50
print(sum([1, 2, 3]))  # This will cause an error

Here, the name sum is redefined as an integer, so Python can no longer use the built-in sum() function. Always avoid using built-in names as identifiers.

Method 5 – Check If a String Is a Valid Identifier in Python

Python provides a handy method called .isidentifier() to check if a string can be used as a valid identifier. This is especially useful when you’re generating variable names dynamically.

# Check if a string is a valid Python identifier
name = "employee_1"
print(name.isidentifier())  # Output: True

invalid_name = "1employee"
print(invalid_name.isidentifier())  # Output: False

You can see the output in the screenshot below.

identifier python

This method returns True if the string follows all the identifier rules. It’s a quick and easy way to validate names in your Python programs.

Common Mistakes Developers Make with Python Identifiers

After years of coding and reviewing other developers’ work, I’ve noticed a few common mistakes:

  • Using too short or meaningless names like x, y, or temp.
  • Mixing different naming styles in the same project.
  • Accidentally overwriting built-in names.
  • Forgetting that identifiers are case-sensitive.

Avoiding these mistakes can save you hours of debugging and confusion later.

My Personal Tips for Writing Better Identifiers in Python

Here are a few tips I personally follow:

  • Use nouns for variables and verbs for functions.
  • Keep identifiers short but meaningful.
  • Stick to one naming convention throughout your project.
  • Use underscores instead of camelCase for functions and variables.
  • Prefix private variables with an underscore (_private_var).

These small habits make your Python code cleaner and more professional.

When you write Python code that others can easily read and understand, you’re not just coding, you’re communicating. Good identifiers are the language of clean, maintainable Python programs.

So next time you start a new Python project, spend a few extra seconds thinking about your variable names. Trust me, your future self (and your teammates) will thank you for it.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.