As you gain experience in Python programming, you will come to a point where you wonder if Python is case sensitive. In other words, the question is whether Python treats uppercase and lowercase letters as distinct or whether it treats them the same. If a language treats them as distinct, we say that the language is case sensitive. If the language treats them as equivalent, we say that the language is case insensitive.
Let us write Python programs to explore this for ourselves!
Python variables are case sensitive
Let us first write a program with two variables that differ only in case.
Here we have created two variables, viz. Kodeclik and kodeclik where the first character of the variable is different in case.
The output is:
Here, we see that the two variables “Kodeclik” and “kodeclik” are treated as separate variables and not confused with each other. Thus, Python is case-sensitive.
Python function names are case sensitive
Let us repeat the same exercise with Python function names.
Here we have created two functions, viz. message and Message where the first character of the function name varies in case. Let us now invoke these functions:
The output is:
The output reveals that the two function names are treated as distinct names and thus function names are case sensitive.
Python reserved keywords are case sensitive
Let us try modifying the case for a Python reserved keyword, like so:
If we run this program, we get the error:
You can see that Python does not recognize “Def” as a reserved keyword and complains about “invalid syntax”. If we modify the first “Def” to a “def”, like so:
Again, “DEF” is not recognized and must be written as “def” because “def” is a reserved word in Python.
Thus Python reserved words are case sensitive because there is exactly one way to write them and that is the only accepted way. A change in case is not even legal in Python.
In overall, we have learnt that Python is a case sensitive language. That being said there is an preferred naming convention for files, function names, and classes that is accepted among programmers. See our Python naming convention blogpost to learn about these styles!
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.