Excerpt
This article explains the conventions and rules around using capital letters to start Python variable names, with code examples.
In Python, variable names follow certain conventions and rules. One common question is whether variables can start with a capital letter or if they must be lowercase. This article will cover the guidelines around capitalization in Python variable names.
Introduction
Python allows some flexibility in naming variables, but also has best practices around conventions. The main rules regarding capitalization for variable names in Python are:
- Lowercase is the preferred standard convention
- Capital letters are used for class names
- Starting with capital letters is technically allowed
- Leading underscores have special meaning and should be avoided
- Python is case sensitive - capital vs lowercase letters matter
Overall, lowercase is the recommended convention for most variables in Python. But capital letters can be utilized in some specific cases.
Lowercase Preferred
Python’s PEP 8 Style Guide recommends using lowercase for variable names with underscores to separate words. For example:
1first_name = "John"
This lowercase convention is the most common and standard in Python code. It maximizes readability and follows Python’s own style guidelines.
So lowercase should be the default choice for most variable names.
Classes Use Capitalization
One major exception where capital letters are used is for class names in Python. Class names follow the CapWords convention of capitalizing the first letter of each word:
1class Student:
2 pass
So capitalization is standard for class names, but not other variables.
Capital Letters Allowed
While lowercase is the predominant convention, names starting with a capital letter are still technically allowed in Python. For example:
1FirstName = "John"
So capital letters can be used in Python variable names, but this goes against the standard conventions.
Free Online String Case Converters
Switching cases in strings and names can be tedious manually. Luckily there are free online tools to quickly convert between cases:
- String to Lowercase
- String to UppercaseFor example, pasting “FirstName” into the lowercase tool returns “firstname”.
These tools help ensure proper casing with minimal effort. Click for more tools
Avoid Starting with Underscores
While internal underscores are fine, avoid using leading underscores in Python variable names. Names beginning with underscores like _name
are reserved for system-defined names in Python.
So don’t start normal variables with underscores - leave those for Python’s internal use only.
Case Sensitivity
Python is a case sensitive language, meaning capital and lowercase letters are treated differently.
For example, firstName
and firstname
would be two separate variables in Python.
So capitalization matters when naming variables in Python.
Consistency for Readability
More important than any convention is consistency within a Python project. Picking one style for naming variables and sticking to it maximizes readability.
Mixing different capitalization styles randomly can harm readability. Find an appropriate convention and stay consistent.
When to Use Capitals
Based on the above points, here are the main cases when capital letters are appropriate in Python variable names:
- Class names (CapWords)
- Constants (like PI)
- Avoid starting normal variables with capital letters
Use capitals for these specific purposes, lowercase for most cases.
Conclusion
While Python allows some flexibility with capitalization, the standard convention and best practice is to:
- Use lowercase for most variables
- Utilize capitals for class names and constants
- Avoid starting normal variables with capital letters or underscores
The most important rule is maintaining consistency of naming within a project for readability.
Leverage online case conversion tools to switch casings easily. Follow Python’s naming best practices to write clean, idiomatic code.
So while capital letters are technically permitted, standard Python style favors lowercase in most cases. Stick to conventions and be consistent above all else.