Kodeclik Blog
Is Python case sensitive?
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.
Kodeclik = "cool"
kodeclik = "wonderful"
print (Kodeclik)
print (kodeclik)
Here we have created two variables, viz. Kodeclik and kodeclik where the first character of the variable is different in case.
The output is:
cool
wonderful
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.
def message(x):
print(x)
def Message(x):
print(x + x)
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:
message("Kodeclik")
Message("Kodeclik")
The output is:
Kodeclik
KodeclikKodeclik
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:
Def message(x):
print(x)
DEF Message(x):
print(x + x)
If we run this program, we get the error:
File "main.py", line 1
Def message(x):
^
SyntaxError: invalid syntax
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:
def message(x):
print(x)
DEF Message(x):
print(x + x)
we get:
File "main.py", line 4
DEF Message(x):
^
SyntaxError: invalid syntax
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!
For more Python content, checkout the math.ceil() and math.floor() functions! Also
learn about the math domain error in Python and how to fix it!
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.