Python 字符串之间的比较

字符串之间的比较。
在Python中可以对字符串直接进行比较,即可以直接进行:

str1='Abch'
str2='Dech'
print str1<str2
print str1==str2
print str1>str2

在Python中,默认是按照ASCII的大小比较的,即从字符串的第一个字符进行比较,如果相等,则继续比较下一个字符,直到分出大小,或者还没分出大小,有一个字符串已经到头了,那么较长的那一个字符串大。
例子如下:

print 'A'>'B' #输出false(-1)
print 'b'>'B' #输出true(1)
print 'Abc'=='Abc' #输出true(0)
print 'Ab'>'Abc'  #输出false
print 'Abc'>'Abbd' #输出true

以上就是Python中对字符串的默认比较方式。

猜你喜欢

转载自blog.csdn.net/I_am_a_coder_coder/article/details/82495097