python⽐⼤⼩代码_Python中如何进⾏字符串⽐较⼤⼩?原标题:Python中如何进⾏字符串⽐较⼤⼩?
在Python中,我们会经常使⽤到字串符,⽤于编码码字。有的时候会需要⽐较字符串⼤⼩。本⽂主要介绍Python字符串⽐较⼤⼩⽅法:字符串的⽐较是⽐较ASCII码值 ,哪个值⼤哪个字符串就⼤。另外也可通过内置函数 ord() 获得每个字符的 Unicode 编码进⾏⼤⼩⽐较。
python字符串之间⽤⽐较符实际上就是⽐较第⼀个字母的ASCII码⼤⼩
str1 = "abc";
str2 = "xyz";
str1>str2
true
通过内置函数 ord() 获得每个字符的 Unicode 编码进⾏⼤⼩⽐较
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
python新手代码图案如何保存# print(ord('10')) TypeError: ord() expected a character, but string of length 2 found
print(ord(' ')) # 32
以上⽐较字符串⼤⼩的⽅法啦,⼤家可以直接套⽤公式使⽤哦~
责任编辑: