Introduction
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
Ninja's Toolkit for 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.
Example of Python Identifiers
The below identifiers are valid as they follow the naming conventions of Python.
- myNum = 101 // variable name follows camelCase
- my_str = “Hello” // variable name follows snake_case
- MY_CONST = 10 // variable name uses all capital letters, which is used for constants
- MYVAR = True // variable name uses all capital letters, which is used for constants
- class MyClass: // follows the naming convention of classes
The below identifiers are not valid in Python as they didn’t follow the naming conventions.
- MyStr = "Ninja"// variable name uses camelcase but begins with a capital letter
- mynum = 3.14 // variable name uses all lowercase letters, which are not recommended in Python
- class my_class: // violates naming convention
- for = 5 // for is a reserved keyword in Python.
Following the naming convention makes the code more readable and maintainable.
Check out this article - Quicksort Python
Frequently Asked Questions
Are identifiers case-sensitive?
Yes, Python is a case-sensitive language, so identifiers are case-sensitive. That is, it considers uppercase and lowercase characters differently. The property is useful while naming identifiers to make the code more readable and maintainable.
Are Python identifiers case-sensitive if the language is strongly typed?
In a strongly typed language, the type of a variable matters when executing operations on it. Strong or weak typing has nothing to do with case sensitivity. So, Python identifiers are case-sensitive even if the language is strongly typed.
Is key value case-sensitive?
Yes, the key value is also case-sensitive language in python. Here we are talking about dictionary keys in Python. However, it is possible that can also implement insensitive keys in Python.
Conclusion
Kudos, Ninja, on successfully completing this reading journey! Now we can conclude Python is a case-sensitive language. We hope this blog has helped you resolve your doubts and enlightened you about the proper ways to deal with identifiers in Python.
Keep learning! We recommend you go through some of our other Python-related articles.:
- Python keywords and identifiers
- Functions in Python
- Interview Questions for Python
- Fibonacci Series in Python
Refer to our Guided Path to upskill yourself in DSA, Competitive Programming, JavaScript, System Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!
But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. For placement preparations, you must look at the problems, interview experiences, and interview bundles.
Best wishes for a fruitful learning experience!