Is Python Case Sensitive?

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

Overview

A programming language is said to be case sensitive if it differentiates between the uppercase and lowercase characters. In this article, we will learn why Python is a case sensitive language and how we can ignore it in Python.

Introduction to Case Sensitivity in Python

While logging into a website, have you ever tried mixing the uppercase and lowercase letters in your password? For example, typing myP@SSword instead of MyP@ssWord. You would have noticed that the uppercase and lowercase letters are not treated as the same and you are not allowed to login if you change the case.

This is an application of case sensitivity. A case sensitive programming language means that the program will treat the uppercase and lowercase letters differently. Hence, we must use the exact case according to the syntax because changing the case, for example from print to Print, will result in an error.

introduction to case sensitivity in python

In the subsequent sections, we will learn whether Python is a case-sensitive language with some examples.

Is Python a Case-Sensitive Language?

YES, Python is a case-sensitive programming language. This means that it treats uppercase and lowercase letters differently. Hence, we cannot use two terms having same characters but different cases interchangeably in Python.

Suppose we write a function calculateArea() to accept a circle's radius and print its area. Let us see how case difference results in error in one case but gives the expected output in the other.

Example 1 (wrong case):

Code:

Output:

NameError: name 'Print' is not defined

Example 2 (right case):

Code:

Output:

Explanation:

Did you notice that only a case difference in print resulted in two different outputs? According to Python's syntax, the keyword print should always be written in lowercase. Hence when we changed its case in example 1, Python could not identify it, resulting in NameError. When we corrected the case in example 2, we got the correct output as expected.

Why is Python Case Sensitive?

Python is called a case sensitive language because the uppercase and lowercase characters are distinguished during execution. Two terms in Python are treated differently if their case is different, even though the characters might be the same. This results in an error if we try to access a value with a different case.

The main reason why Python is structured this way is that Python finds its applications in various fields. We would not want to restrict the number of identifiers and symbols which can be used, hence case sensitivity is allowed. In fact, most of the popular high level programming languages like Java, C, C++ and JavaScript are case sensitive.

Variable Naming Standards in Python: When to Use Upper or Lower Case?

There are some variable naming standards that we should follow while writing a Python program. These are not entirely mandatory but they make our code cleaner and more readable.

  • Variable and Function names should be in lowercase, with words separated by underscores to improve readability. Ex: circle_radius = 5
  • Packages and Modules should also be written in lowercase. Ex: import math
  • Class names should have the first letter of every word in uppercase. They should not be separated by an underscore. Ex: class AreaCalculator
  • Constants should entirely be in uppercase and should use underscore to separate words. Ex: PI = 3.1416

Note:

While the above naming standards are highly recommended and good coding practices, remember that it will not lead to any errors if we do not follow them strictly.

How to Ignore Case in Python?

In most scenarios, the username for logging into a website is not case sensitive. Suppose my username is python-user, I should be able to login even if I type something like Python-User or PYTHON-USER. How can we make Python ignore the case while checking for equality? We can make use of the .upper() or .lower() methods in Python to change the case of a string.

  • .upper(): It converts all the characters in a given string to uppercase.
  • .lower(): It converts all the characters in a given string to lowercase.

Suppose we need to design a login page where the password is case sensitive but the username ignores the case. We will take both inputs from the user, convert the username to uppercase (or lowercase) and check for its equality with the expected username, which is also converted to uppercase (or lowercase). The password is case sensitive, hence we do not need to convert it to upper or lowercase.

For the username, Python will only check if the string matches by character, ignoring the cases of the input and the expected string. On the contrary, the password check will include both the character and the case matching. Let's see this in action:

Code:

Output:

Explanation:

In Scenario 1, the case of the username is ignored using .lower() method. Hence, login is successful even though the case of the username inputted by the user and that in the records is different. In Scenario 2, we do not use .lower() or .upper() methods. Hence the case is not ignored and the equality check considers the cases of both usernames. The login is unsuccessful since the case of both usernames are different.

Note:

In the above example, we have simplified the login scenario by assuming that there is only one correct username and password combination. We have not used .lower() or .upper() to ignore the cases of the password since passwords are always case sensitive.

Conclusion

  • A programming language is said to be case sensitive if it distinguishes between uppercase and lowercase letters.
  • Python is a `case sensitive programming language.
  • Variable, functions, modules and package names are written in lowercase by convention.
  • Class names and constants are written in uppercase.
  • We can ignore case in Python using the .upper() and .lower() methods.
  • .upper() converts the entire string to uppercase whereas .lower() converts a string to lowercase.
  • Python is case sensitive to allow more identifier names, given its wide number of applications in various fields.

See Also: