Is Python case-sensitive or case-insensitive?


In this article, we will learn whether Python is case-sensitive or case-insensitive.

What is Case Sensitivity?

If a programming language differentiates between uppercase and lowercase characters, it is said to be case−sensitive.

Have you ever tried mixing uppercase and lowercase letters in your password when login into a website? For example, password instead of tutorialspoint, use TutorialsPOINT. You may have observed that uppercase and lowercase letters are not considered the same, and changing the case stops you from logging in.

This is an example of case sensitivity in action. A case-sensitive programming language treats uppercase and lowercase letters differently. As a result, we must use the syntax's exact case because changing the case, for example, from print to Print, will result in an error.

Is Python a Case-Sensitive Language?

Yes, Python is a case−sensitive programming language. This means that it considers uppercase and lowercase letters differently. As a result, in Python, we cannot use two terms with the same characters but different cases interchangeably.

Code 1- Wrong Case

The following program throws NameError as the print statement is invalid(P in uppercase) −

Example

length = 5
breadth= 2

area_rectangle = length*breadth
Print("Area of Rectangle = ", area_rectangle)

Output

On execution, the above program will generate the following output −

Traceback (most recent call last):
  File "main.py", line 5, in 
    Print("Area of Rectangle = ", area_rectangle)
NameError: name 'Print' is not defined

Code 2- Right Case

Example

The following program returns the area rectangle and executes without any errors −

length = 5
breadth= 2

area_rectangle = length*breadth
print("Area of Rectangle = ", area_rectangle)

Output

On execution, the above program will generate the following output −

Area of Rectangle =  10

Did you notice how a case difference in print resulted in two distinct outputs? The keyword print should always be used in lowercase, according to Python syntax. As a result, when we altered its case in Code 1, Python was unable to recognize it, resulting in a NameError. When we fixed the case in Code 2, we obtained the expected result.

Why is Python Case Sensitive?

Python is known as a case-sensitive language because it differentiates between uppercase and lowercase characters during execution. Python treats two terms differently if their cases change, even though the characters are the same. If we try to retrieve a value with a different case, we get an error.

The fundamental reason Python is built this way is that it has applicability in a variety of fields. We do not wish to limit the number of identifiers and symbols that can be used, hence case sensitivity is permitted. In reality, most high-level programming languages, such as Java, C, C++, and JavaScript, are case-sensitive.

Variable Naming Conventions in Python: When Should You Use Upper or Lower Case?

While writing Python code, we need to follow particular variable name conventions. These are optional, but they make our code clearer and more readable.

  • To improve readability, variable and function names should be in lowercase, with terms separated by underscores. For example, input_number = 10.

  • Packages and Modules should be written in lowercase as well. For example, import math.

  • The first letter of each word in a class name should be uppercase. They should not be separated by an underscore. For example, ExampleClass.

  • Constants should be all uppercase and use underscore to separate words. For example, PI = 3.1416.

NOTE

The above naming conventions are highly suggested for good coding practices, note that not fully following them will not result in any errors.

In Python, How do you ignore cases?

In most cases, the username used to log in to a website is not case-sensitive. If my username is tutorials−point. I should be able to log in even if I enter Tutorials−Point or TUTORIALS−POINT. How can we force Python to ignore the case when checking for equality? To change the case of a string, we can utilize Python's .upper() and .lower() functions.

  • upper() − This function converts all characters in a specified string to uppercase.

  • lower() − This function converts all characters in a specified string to lowercase.

Assume we need to create a login page where the password is case-sensitive but the username is case-insensitive. We will take both user inputs, transform the username to uppercase (or lowercase), and compare it to the desired username, which is likewise converted to uppercase (or lowercase). We don't need to convert the password to upper or lowercase because it is case-sensitive.

Python will just check if the string matches by character for the username, ignoring the case of the input and the expected string. The password check, on the other hand, will contain both character and case matching. Let's put this into code −

Example

The following program slows the difference between case-sensitive and case-insensitive −

input_username = "Tutorials-Point"
gvn_username = "tutorials-point"

input_password = "sampleP@SSword"
gvn_password = "sampleP@SSword"

# here we are converting the input_username into lowercase  
print("Case 1: Case Ignored(case-insensitive)")
if (input_username.lower() == gvn_username.lower() and input_password == gvn_password):
    print("You are logged in Successfully!!")
else:
    print("Incorrect Username or Password")

print()

# here we are directly checking whether the input_username and password
# are equal to the gvn_username and gvn_password
print("Case 2: Case Not Ignored(case-sensitive)")
if (input_username == gvn_username and input_password == gvn_password):
    print("You are logged in Successfully!!")
else:
    print("Incorrect Username or Password")

Output

On execution, the above program will generate the following output 

Case 1: Case Ignored(case-insensitive)
You are logged in Successfully!!

Case 2: Case Not Ignored(case-sensitive)
Incorrect Username or Password

The case of username is ignored in Case 1 by utilizing the .lower() function. As a result, even though the case of the username entered by the user and that in the records differs, the login is successful. We don't use the .lower() or .upper() methods in Case 2. As a result, the case is not ignored, and the equality check takes into account the cases of both usernames. The login fails because the cases of both usernames are different.

NOTE

We simplified the login problem in the preceding example by assuming that there is only one valid username and password combination. We did not use .lower() or .upper() to ignore the password's case because passwords are always case-sensitive.

Conclusion

We hope you are no longer puzzled by the most important aspects of Python case sensitivity. You are now familiar with some good Python case-sensitive naming practices. You now understand how to ignore the case in Python when performing case-insensitive string comparisons.

Updated on: 26-Dec-2022

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements