這篇文章給大家分享的是有關(guān)Python中進(jìn)行字符串比較大小的方法的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過來看看吧。
python字符串之間用比較符實(shí)際上就是比較第一個(gè)字母的ASCII碼大小
str1 = "abc"; str2 = "xyz"; str1>str2 true
通過內(nèi)置函數(shù) ord() 獲得每個(gè)字符的 Unicode 編碼進(jìn)行大小比較
print(max(['1', '2', '3'])) # 3 print(max(['31', '2', '3'])) # 31 print(max(['13', '2', '3'])) # 3 print(max(['10', '11', '12'])) # 12 print(max(['13', '11', '12'])) # 13 print(ord('1')) # 49 print(ord('2')) # 50 print(ord('3')) # 51 # print(ord('10')) TypeError: ord() expected a character, but string of length 2 found print(ord(' ')) # 32
感謝各位的閱讀!關(guān)于Python中進(jìn)行字符串比較大小的方法就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!