Python字符串比较

Python String comparison can be performed using equality (==) and comparison (<, >, !=, <=, >=) operators. There are no special methods to compare two strings.

可以使用相等(==)和比较(<,>,!=,<=,> =)运算符执行Python字符串比较。 没有比较两个字符串的特殊方法。

Python字符串比较 (Python String Comparison)

Python string comparison is performed using the characters in both strings. The characters in both strings 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:

输出:

True
False
False
False
True
True

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:
Apple
Please enter the name of second fruit:
Banana
Apple comes before Banana in the dictionary.

Please enter the name of first fruit:
Orange
Please enter the name of second fruit:
Orange
Orange 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:

输出:

False
True
A unicode is 65 ,a unicode is 97

So “Apple” is smaller when compared to “apple” because of their Unicode values. We are using ord() function to print the Unicode code point value of the characters.

因此,由于其Unicode值,“ Apple”与“ apple”相比要小一些。 我们正在使用ord()函数来打印字符的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:

输出:

False
False

Obviously, both the strings are neither smaller nor greater than the other one. Hence the output is false in both the cases.

显然,两个字符串都不小于也不大于另一个。 因此,在两种情况下输出都是错误的。

GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

翻译自: https://www.journaldev.com/23511/python-string-comparison

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值