October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Identifiers in Python: Naming Rules, Examples, and Best Practices

Python names must start with a letter or underscore, cannot be reserved keywords, and are case-sensitive. Learn the identifier rules, Unicode caveats, and PEP 8 style conventions.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Python identifiers—the names used for variables, functions, classes, and other objects—must start with a letter or underscore, may include digits after the first character, and cannot be reserved keywords. Python also accepts many Unicode letters, but normalization and look-alike characters mean that ASCII names are often the clearest choice for shared code. The syntax rules come from the language; conventions such as snake_case come from PEP 8.

What makes a name a valid Python identifier?

The Python 3.14.7 Language Reference defines a name as a starting character followed by zero or more continuation characters. A name must contain at least one character, and the language imposes no maximum length. The reference states: “Names must contain at least one character, but have no upper length limit.” Python Language Reference: Names (identifiers and keywords).

  • A name can begin with an ASCII letter (A–Z or a–z) or an underscore (_).
  • A digit can appear after the first character, but not at the beginning.
  • Eligible non-ASCII characters can also be used.
  • Names are case-sensitive: item and Item are different identifiers.
Example Valid? Reason
count2 Yes A digit appears after the first character.
_cache Yes An underscore may start a name.
2count No A name cannot start with a digit.
item and Item Both valid, distinct Python distinguishes uppercase and lowercase letters.

Which names are forbidden or context-dependent?

Reserved keywords

Keywords have a defined grammatical role and cannot be used as ordinary identifiers. Examples include class, def, for, if, import, return, and while, as well as True, False, and None. The complete set can vary between Python versions. To inspect the interpreter you are running, use the keyword module:

import keyword
print(keyword.kwlist)

Check the target Python interpreter when code needs to account for its exact keyword set.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Soft keywords

Soft keywords have a special meaning only in particular grammar contexts, so their spellings can still be identifiers elsewhere. The language reference describes match, case, and _ as soft keywords in relevant contexts. For example, _ is a wildcard in a case pattern; that role does not make it a reserved keyword everywhere.

Can Python identifiers contain Unicode?

Yes. Python permits Unicode characters that meet its identifier rules, including letters from many writing systems. The language reference gives ř_1, 蛇, and साँप as valid examples. It also shows that Unicode support does not allow every symbol: r〰2, €, and 🐍 are invalid identifier examples. See the identifier character rules in the Python 3.14.7 Language Reference.

Normalization can make different spellings equivalent

Python normalizes identifiers to NFKC while parsing. As a result, two character sequences that normalize to the same form can denote the same name. A typographic spelling is not necessarily a distinct identifier just because it looks different on screen. The behavior is specified in the Language Reference and explained in PEP 3131.

Look-alike characters can confuse readers

Some letters from Latin, Greek, and Cyrillic scripts look alike while remaining different characters and distinct identifiers. PEP 672 discusses these Unicode confusables and their security implications: PEP 672: Unicode-related security considerations. This is a reason to review unfamiliar names carefully, not a reason to treat every Unicode identifier as unsafe. In a multilingual codebase, agree on consistent script use and choose names that teammates can reliably recognize.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What naming style should you use?

Syntax determines what Python accepts; style helps people understand the code. PEP 8 recommends these conventions:

Name’s role PEP 8 convention Example
Variable or function Lowercase words separated by underscores (snake_case) item_count, load_record()
Class Capitalized words joined together (CapWords) CustomerRecord
Constant Uppercase words separated by underscores MAX_RETRIES
Module Generally short and lowercase; underscores may improve readability data_tools

These are conventions, not parser requirements. PEP 8 says that “Names that are visible to the user as public parts of the API should follow conventions that reflect usage rather than implementation.” Read more in PEP 8: Naming Conventions.

Handle awkward names without making them cryptic

  • If an argument would conflict with a keyword, PEP 8 recommends adding one trailing underscore, such as class_, rather than abbreviating or distorting the word.
  • Avoid single-character lowercase l, uppercase O, and uppercase I when a font could make them look like digits.
  • For public names, prioritize how callers use and understand the API over the details of its implementation.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How to choose between two possible names

When both candidates look plausible, evaluate them in this order:

  1. Check the grammar: does the name start with an allowed character and use only allowed continuation characters?
  2. Check for a keyword conflict: is it reserved in the Python version the code targets?
  3. Match the role: use the PEP 8 convention for a variable, function, class, constant, or module.
  4. Read it at the call site: choose the spelling that makes the code’s purpose clear to its intended readers.
  5. Consider Unicode effects: avoid relying on normalization differences or visually confusable characters to distinguish names.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.