How to Define a Constant in Python
Python has no const keyword. A constant is an ordinary variable whose name is written in capital letters at the top of a module, such as ISSUER = "https://login.example.org", which tells every reader not to change it.
Syntax
<NAME> = <value>
| Part | What it does |
|---|---|
<NAME> | The constant's name, in capital letters with underscores between words, such as MAX_RETRIES. |
<value> | The value the program uses everywhere and never changes. |
Python's style guide, PEP 8, sets the convention: "Constants are usually defined on a module level and written in all capital letters with underscores separating words. Examples include MAX_OVERFLOW and TOTAL."
The Example
The example defines two constants, uses them, and then assigns a new value to one of them, to show what Python does about it.
Prerequisites
- uv and Python. This guide uses uv 0.13.10 with Python 3.15.2.
Step 1: Write the Code
Create a file named main.py:

ISSUER = "https://login.example.org"
MAX_RETRIES = 3
print(ISSUER)
print(MAX_RETRIES * 2)
ISSUER = "https://elsewhere.example.org"
print(ISSUER)
Step 2: Run It
uv run python main.py
https://login.example.org
6
https://elsewhere.example.org

The command is the same in zsh, bash and PowerShell.
What Happened
- Both constants worked like any variable:
ISSUERprinted its URL, andMAX_RETRIES * 2printed6. - Assigning a new value to
ISSUERworked without an error. Python doesn't enforce constants. The capital letters are a promise between programmers, not a rule the language checks.
That's why the convention matters: a reader who sees ISSUER knows every part of the program expects the same value, and that changing it means changing the program.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Expecting Python to stop a change | The new value silently replaces the old one | Treat capital-letter names as read-only, and let a type checker help |
| Defining a constant inside a function | It's created again on every call and other functions can't see it | Define it at the top of the module |
| Using a list as a constant | The name stays the same, but anyone can still add to the list | Use a tuple for a fixed sequence |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| A "constant" has an unexpected value | Some code assigned to it | Search the code for NAME = |
NameError when a function uses the constant | The constant is defined after the code that runs, or in another module without an import | Define it at the top, or import it |
FAQ
Is there a way to make Python enforce a constant?
Not at run time. The typing module has Final, which the documentation calls a "Special typing construct to indicate final names to type checkers": a type checker reports an assignment to a Final name, but Python still runs it.
Where do I put constants that several modules share?
In one module, for example settings.py, and import them from there.
Conclusion
A Python constant is a module-level name in capital letters. Python won't stop anyone from changing it, so the name itself does the job: it tells every reader the value is fixed.
Additional Resources
- PEP 8: Constants, the style guide's naming rule.
- typing.Final, in the Python documentation.
