本文共 3018 字,大约阅读时间需要 10 分钟。
Python String comparison can be performed using equality (==) and comparison (<, >, !=, <=, >=) operators. There are no special methods to compare two strings.
可以使用相等(==)和比较(<,>,!=,<=,> =)运算符执行Python字符串比较。 没有比较两个字符串的特殊方法。
Python string comparison is performed using the characters in both strings. The characters in both are compared one by one. When different characters are found then their Unicode value is compared. The character with lower Unicode value is considered to be smaller.
使用两个字符串中的字符执行Python字符串比较。 两个中的字符被一一比较。 当找到不同的字符时,将比较它们的Unicode值。 Unicode值较低的字符被认为较小。
Let’s look through some examples for string comparison.
让我们看一些用于字符串比较的示例。
fruit1 = 'Apple'print(fruit1 == 'Apple')print(fruit1 != 'Apple')print(fruit1 < 'Apple')print(fruit1 > 'Apple')print(fruit1 <= 'Apple')print(fruit1 >= 'Apple')
Output:
输出:
TrueFalseFalseFalseTrueTrue
Both the strings are exactly the same, hence they are equal. So equality operator is returning True in this case.
两个字符串完全相同,因此它们相等。 因此,在这种情况下,相等运算符将返回True。
Let’s look at another example where we will get inputs from the user and then compare them.
让我们看另一个示例,在该示例中,我们将从用户那里获取输入,然后进行比较。
fruit1 = input('Please enter the name of first fruit:\n')fruit2 = input('Please enter the name of second fruit:\n')if fruit1 < fruit2: print(fruit1 + " comes before " + fruit2 + " in the dictionary.")elif fruit1 > fruit2: print(fruit1 + " comes after " + fruit2 + " in the dictionary.")else: print(fruit1 + " and " + fruit2 + " are same.")
Output:
输出:
Please enter the name of first fruit:ApplePlease enter the name of second fruit:BananaApple comes before Banana in the dictionary.Please enter the name of first fruit:OrangePlease enter the name of second fruit:OrangeOrange and Orange are same.
Let’s see if the comparison is case sensitive or not? Also if ‘a’ comes ‘A’?
让我们看看比较是否区分大小写? 另外,如果“ a”是“ A”?
print('apple' == 'Apple')print('apple' > 'Apple')print('A unicode is', ord('A'), ',a unicode is', ord('a'))
Output:
输出:
FalseTrueA unicode is 65 ,a unicode is 97
So “Apple” is smaller when compared to “apple” because of their Unicode values. We are using to print the Unicode code point value of the characters.
因此,由于其Unicode值,“ Apple”与“ apple”相比要小一些。 我们正在使用来打印字符的Unicode代码点值。
What if one of the string is made of second string and some additional characters?
如果其中一个字符串由第二个字符串和一些其他字符组成,该怎么办?
print('Apple' < 'ApplePie')
Output: True
输出: True
If the characters sequence are the same in both the strings but one of them have some additional characters, then the larger length string is considered greater than the other one.
如果两个字符串中的字符序列相同,但其中一个具有一些附加字符,则认为长度较大的字符串要大于另一个字符串。
What if we use < and > operators to compare two equal strings?
如果我们使用<和>运算符比较两个相等的字符串怎么办?
print('apple' < 'apple')print('apple' > 'apple')
Output:
输出:
FalseFalse
Obviously, both the strings are neither smaller nor greater than the other one. Hence the output is false in both the cases.
显然,两个字符串都不小于也不大于另一个。 因此,在两种情况下输出都是错误的。
翻译自:
转载地址:http://oamzd.baihongyu.com/