博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python字符串比较
阅读量:2531 次
发布时间:2019-05-11

本文共 3018 字,大约阅读时间需要 10 分钟。

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 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.

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

. 检出完整的python脚本和更多Python示例。

翻译自:

转载地址:http://oamzd.baihongyu.com/

你可能感兴趣的文章
Jenkins 忘记admin密码拯救方法
查看>>
如何优雅的使用RabbitMQ
查看>>
.NET webAPI中集成swagger
查看>>
AutoFac使用方法总结:Part I
查看>>
mvn install selenium依赖包
查看>>
关于SQL的相关笔记【长期更新,只发一帖】
查看>>
linux awk命令详解
查看>>
android:id="@+id/button1" 与 android:id="@id/button1" 区别 @string
查看>>
下载网页或者外网视频
查看>>
.Net Mvc3框架调用服务端控件解决方案
查看>>
图像处理基础(3):均值滤波器及其变种
查看>>
构建之法阅读笔记06
查看>>
内部培训-流程图培训
查看>>
Struts 2读书笔记------struts 2的标签
查看>>
Hibernate读书笔记-----Hibernate的关联映射之组件属性关联关系
查看>>
iOS SKStoreProductViewController的应用
查看>>
我的发帖小结 csdn by titer1 2012年9月截止
查看>>
过滤sql注入
查看>>
POJ 2562 Primary Arithmetic
查看>>
关于VS2013的编码的UI测试。
查看>>