Is Python Case Sensitive While Dealing with Identifiers?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Yes, Python is a case-sensitive language, i.e., it treats uppercase and lowercase characters differently. This applies to identifiers too. You must avoid using the same name with different cases while naming identifiers. It is always good to use names that are more easily understood. If you want to store counts of 2 values, you would rather name your variables count1 and count2 instead of naming them C and c. Although the latter would give you separate variables, it can cause a lack of understanding in the longer run.

Rules for Identifiers in Python

To construct a Python identifier, a few requirements must be fulfilled.

  • Reserved keywords (special words used for a specific purpose, e.g., while, for, if, etc.) cannot be used as identifier names.
  • Python identifiers can contain lowercase letters (a-z), uppercase letters (A-Z), numerals (0-9), and underscores ( _ ). This means that Identifiers in Python cannot contain special characters except underscore in them.
  • The name of the identifier cannot begin with a number.
  • The name of a Python identifier can begin with an underscore.
  • The length of the identifier name is unrestricted.
  • The names of Python identifiers are case sensitive.

Example of Python Identifiers

Based on the rules mentioned above, identifiers in Python can be created. To understand this better, let's consider a few examples as shown below:

  • Valid Identifiers

    • count1 : contains only letters and numbers
    • count_oddNo : contains all valid characters
    • _ : underscore is a valid identifier
    • _count : identifier can start with an underscore
    • _5 : valid since it starts with an underscore and has legal characters
    • my_Dog_Loves_Cookies_and_Ham_and_Cheese: valid despite the length as there are no restrictions on length.
  • Invalid Identifiers

    • for : no keywords allowed
    • a+b : no other special characters except underscore allowed
    • 200 : identifier cannot contain only a numeral
    • 2Abs : identifier cannot begin with a number

Python Identifier Naming Best Practices

  • For Constants:
    • Use all uppercase characters while naming identifiers.
    • An underscore can be used as a separator for words in the constant name.
    • Example: SUM_OF_ODD, MAX_LENGTH, etc.
  • For Class Names:
    • Names of classes in Python should start with uppercase letters.
    • Use Camelcase, i.e. every word in an identifier should begin with a capital letter to separate words and enhance readability.
    • Example: StudentName, MobileNo, etc.
  • Private Variables:
    • Usually, underscores are used to start Python built-in types. You can avoid this except while using a private variable.
    • Example: _digit
  • Package Names:
    • Use lowercase while naming packages.
    • Avoid the use of underscores.
    • Example: math, utilities, etc.
  • Miscellaneous:
    • When naming variables, functions, and modules, use lowercase letters. Use underscore as a separator in such cases. Example: is_complete(), emp_salary, etc. Alternatively, if preferred, you can also use camel casing for naming. For example, the above names can be written as isComplete(), empSalary, etc.
    • Do not have two underscores at the start and end of the identifier name. Such naming is only used by a language-defined keyword.
    • For boolean functions, it is always a good practice to start the function name with is. This helps us to get a yes-no answer. Example: is_travelling(), is_paid(), etc. If camel case is used, these could be written as isTravelling(), isPaid(), etc.
    • There is no limit on the length of an identifier name.
    • Always keep an identifier name small and concise to avoid the complexity of remembering names.

Learn More:

Conclusion

  • Identifiers in Python are names used to identify or define data structures.
  • Identifiers in Python are case-sensitive.
  • Python identifier names can contain letters, digits, or underscores only.
  • While naming identifiers in Python, proper naming conventions should be followed to ensure understandability.