In this article, we will learn whether Python is case-sensitive or case-insensitive.
If a programming language distinguishes between uppercase and lowercase characters, then it is said to be case-sensitive.
Have you ever tried to mix uppercase and lowercase letters in your password when logging into a website? For example, use TutorialsPOINT instead of tutorialspoint as the password. You may observe that uppercase letters and lowercase letters are considered different and changing case will prevent you from logging in.
This is an example of case sensitivity in action. Case-sensitive programming languages distinguish between uppercase and lowercase letters. Therefore, we must use the exact case of the syntax, since changing case, for example from print to Print, will cause an error.
Yes, Python is a case-sensitive programming language. This means it distinguishes between uppercase and lowercase letters. Therefore, in Python we cannot use two terms with the same characters but different cases interchangeably.
The following program throws NameError as error because the print statement is invalid (capital P) −
The Chinese translation oflength = 5 breadth= 2 area_rectangle = length*breadth Print("Area of Rectangle = ", area_rectangle)
When executed, 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
The following program returns the area of a rectangle and is executed without any errors -
length = 5 breadth= 2 area_rectangle = length*breadth Print("Area of Rectangle = ", area_rectangle)
When executed, the above program will generate the following output -
Area of Rectangle = 10
Have you ever noticed that the difference in upper and lower case produces two different results on the printed output? According to Python syntax, the keyword print should always be lowercase. So when we changed its case in Code 1, Python didn't recognize it, resulting in a NameError. When we fixed the casing in Code 2, we got the expected results.
Python is known as a case-sensitive language because it distinguishes between upper and lower case characters during execution. Even if the characters are the same, Python will treat the two terms as different when case changes. If we try to retrieve the value using different case, we will get an error.
The fundamental reason why Python is built this way is that it has applicability in various fields. We do not want to limit the number of identifiers and symbols that can be used, so case sensitivity is allowed. In fact, most high-level programming languages, such as Java, C, C++, and JavaScript, are case-sensitive.
When writing Python code, we need to follow specific variable naming conventions. These are optional, but they make our code clearer and readable.
To improve readability, variable and function names should use lowercase letters and be separated by underscores. For example, input_number = 10.
Package and module names should also use lowercase letters. For example, import math.
The first letter of each word in the class name should be capitalized. They should not be separated by underscores. For example, ExampleClass.
Constants should be all uppercase and use underscore to separate words. For example, PI = 3.1416.
The above naming conventions are highly recommended for good coding practice, but please note that not following them at all will not result in any errors.
In most cases, the username used to log into the website is not case-sensitive. If my username is tutorials−point. Even if I enter Tutorials−Point or TUTORIALS−POINT, I should be able to log in. How can we force Python to ignore case when checking equality? To change the case of a string, we can utilize Python’s .upper() and .lower() functions.
upper() − This function converts all characters in the specified string to uppercase.
lower() − This function converts all characters in the specified string to lowercase.
Suppose we need to create a login page where the password is case-sensitive but the username is not case-sensitive. We will take the username and password entered by the user, convert the username to uppercase (or lowercase), and compare it to the desired username, which is also converted to uppercase (or lowercase). We don't need to convert the password to uppercase or lowercase as it is case sensitive.
Python will check if the username matches by characters only, ignoring the case of the input and expected strings. Password checking, on the other hand, will include character and case matching. Let's put this into code -
下面的程序减慢了区分大小写和不区分大小写之间的差异 -
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")
在执行上述程序时,将生成以下输出 −
Case 1: Case Ignored(case-insensitive) You are logged in Successfully!! Case 2: Case Not Ignored(case-sensitive) Incorrect Username or Password
在案例1中,通过使用.lower()函数,忽略了用户名的大小写。因此,即使用户输入的用户名和记录中的用户名的大小写不同,登录仍然成功。在案例2中,我们不使用.lower()或.upper()方法。因此,大小写不被忽略,相等性检查考虑了两个用户名的大小写。由于两个用户名的大小写不同,登录失败。
我们在上面的示例中简化了登录问题,假设只有一个有效的用户名和密码组合。我们没有使用.lower()或.upper()来忽略密码的大小写,因为密码始终区分大小写。
我们希望你不再对Python大小写敏感的最重要方面感到困惑。你现在熟悉了一些良好的Python大小写敏感的命名规范。你现在明白了在Python中如何忽略大小写进行大小写不敏感的字符串比较。
The above is the detailed content of Is Python case-sensitive or case-insensitive?. For more information, please follow other related articles on the PHP Chinese website!