A string is a very useful and popular variable type used in the Python programming language. Sometimes we need to operate two or more strings and compare them. Comparing numbers is easy but the string is a bit different and harder. In this tutorial, we will look at different comparison operations on Python strings.
字符串是Python编程语言中非常有用且流行的变量类型。 有时我们需要操作两个或多个字符串并进行比较。 比较数字很容易,但是字符串有点不同并且更难。 在本教程中,我们将研究对Python字符串的不同比较操作。
查找更大的字符串 (Find Bigger String)
Two numbers can be compared in order to find the bigger one but how can we find a bigger string. Strings are consist of multiple characters and evaluated according to these multiple characters. Comparing two string means comparing the ASCII values of each character one by one. For example:
可以比较两个数字以找到更大的数字,但是如何找到更大的字符串。 字符串由多个字符组成,并根据这些多个字符进行评估。 比较两个字符串表示一个一个地比较每个字符的ASCII值。 例如:
a='abc'
b='abd'
b > a
![Find Bigger String](https://i-blog.csdnimg.cn/blog_migrate/63b09a7733c733503de1c8089fb30b9d.png)
These two strings are very similar but the third character is different for both. If we compare these two strings b
will be bigger than a
the variable because of d
ASCII value is bigger than c
ASCII value. And as a result, a boolean value True
is returned.
这两个字符串非常相似,但是第三个字符却不同。 如果我们比较这两个字符串,则b
将大于a
变量,因为d
ASCII值大于c
ASCII值。 结果,返回布尔值True
。
检查是否与==运算符相同 (Check If Same with == Operator)
The most popular comparison operation is checking the two strings if they are the same. We will use ==
operator in order to check equality.
最受欢迎的比较操作是检查两个字符串是否相同。 我们将使用==
运算符来检查相等性。
a='abc'
b='abd'
a == b
#False
a = 'abd'
a == b
#True
![Check If Same](https://i-blog.csdnimg.cn/blog_migrate/88109da8f5928fc58308d9538b30faae.png)
检查是否与运算符相同(Check If Same with is Operator)
Python programming language also provides is
operator to check given objects are the same. We can use is
operator in order to check if given two string is the same which means two string provides the same characters in the same order.
Python编程语言也提供了is
运营商咨询是否给予对象是相同的。 我们可以使用is
运算符来检查给定的两个字符串是否相同,这意味着两个字符串以相同的顺序提供相同的字符。
a='ismail'
b='poftut'
c='ismail'
a is b
#False
b is a
#False
a is c
#True
c is a
#True
![Check If Same with is Operator](https://i-blog.csdnimg.cn/blog_migrate/c0f1631d1f46cfcb2722d9f0c924a796.png)
Check If Same with is Operator
检查是否与运算符相同
检查是否与!=运算符不同(Check If Different with != Operator)
The reverse of checking equality is checking if two strings are different. If the strings are different from each other this will return boolean True
. We will use !=
operator for these operations.
检查相等性的相反操作是检查两个字符串是否不同。 如果字符串彼此不同,则将返回布尔True
。 我们将使用!=
运算符进行这些操作。
a = 'abd'
b = 'abd'
a != b
#False
b = 'abc'
a != b
#True
![Check If Different](https://i-blog.csdnimg.cn/blog_migrate/1ac648291faf92caf1ff4f97371b7b59.png)
检查字符串是否为空(Check If String Empty)
Strings can provide different characters but also can contain no value or just spaces. We can check if a string is empty or not by using strip
function. We will strip spaces from the string and check equality with an empty string literal like below.
字符串可以提供不同的字符,但也可以不包含任何值或仅包含空格。 我们可以使用strip
函数检查字符串是否为空。 我们将删除字符串中的空格,并使用如下所示的空字符串文字检查是否相等。
a = 'abc'
b = ' '
b.strip() == ''
#True
a.strip() == ''
#False
![Check If String Empty](https://i-blog.csdnimg.cn/blog_migrate/6631ff59984cd1f784271cba2ada6c04.png)
We can also use the string with the if
keyword. If the string has a value different than space or empty the if will get a True
boolean value.
我们还可以将字符串与if
关键字一起使用。 如果字符串的值与空格不同或为空,则if将获得True
布尔值。
a = 'abc'
b = ' '
if b:
print("Empty")
#Empty
if a:
print("Not empty")
#Not empty
![Check If String Empty](https://i-blog.csdnimg.cn/blog_migrate/110bafb9094389fa5663f616e403b587.png)
比较不区分大小写的字符串(Compare String Case Insensitive)
In some cases, we may have strings those need to be compared in a case insensitive manner. We can use some function provided by the Python string type like lower()
and upper()
. In this example, we will lower all provided strings and compare them with == and != operators.
在某些情况下,我们可能有一些字符串需要以不区分大小写的方式进行比较。 我们可以使用Python字符串类型提供的某些函数,例如lower()
和upper()
。 在此示例中,我们将降低所有提供的字符串,并将它们与==和!=运算符进行比较。
a='isMail'
b='Poftut'
c='iSmaiL'
a.lower() == b.lower()
#False
a.lower() != b.lower()
#True
a.lower() != c.lower()
#False
a.lower() == c.lower()
#True
b.lower() == c.lower()
#False
![Compare String Case Insensitive](https://i-blog.csdnimg.cn/blog_migrate/84819e22562db973e81b2d44d2fba4a3.png)