Yes, Python is a case-sensitive language. It considers that uppercase and lowercase characters are different. It is also applicable to identifiers. The property is useful while naming identifiers to make the code more readable and maintainable.
For example, variables named "my_variable", "My_Variable", and "MY_VARIABLE" would be treated as different identifiers in Python.
Python is a highly popular programming language. The programming language follows its own set of rules or grammar and conventions for naming its identifiers. A query that often pops up for novice programmers is whether Python is a case-sensitive language while dealing with identifiers.
The article below will help you know is python case sensitive when dealing with identifiers.
Rules for Identifiers in Python
While naming identifiers in Python, specific rules are to be followed that state the use of characters, case of letters, capitalization, and reserved words.
- Python has a total of 35 reserved keywords (Ex. "If", "else", "def", "while", "for", etc.) that are used to define the syntax of the programming language. These unique words cannot be used as names for identifiers.
- All identifiers declared in the same scope should be distinct.
- While naming identifiers, you can use a combination of lowercase characters (a-z), uppercase case characters (A-Z), digits (0-9), and underscore characters (_).
- Names of identifiers in Python are case-sensitive. Ex. "myPet" and "mypet" are different.
- Special characters like "@, $, %, &, *, #" are not allowed in identifier names.
- An identifier name must start with a non-numeric character. That is, the underscore can be the first character of an identifier name.
- The identifier name can be of any length.
You can also practice with the help of Online Python Compiler
Naming Identifiers in Python
Certain naming conventions are to be followed while naming identifiers in Python to make the code more readable.
For class names
- Class names in Python should always begin with an uppercase letter.
- Class names follow camelcase. That means the first letter of each word of the identifier should be capitalized.
- Ex. MyClass
For package names
- Package names should be in lowercase.
- It is recommended to avoid using underscores
- Ex. math
For constants
- While naming a constant, all letters should be in uppercase
- An underscore can be used as a separator for words
- Ex. My_CONSTANT
For private variables
- Use underscores at the beginning of the private variables.
- Ex. _str
Miscellaneous
- For boolean identifiers, it is recommended to begin the function name with is followed by an underscore and then the function name. Ex. is_Prime().
- Identifier names should be small, concise, and meaningful.