python字符串比较大小

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_40803626/article/details/93405170

理解原理和相关知识

  1. 字符串按位比较,两个字符串第一位字符的ascii码谁大,字符串就大,不再比较后面的;第一个字符相同就比第二个字符串,以此类推,需要注意的是空格的ascii码是32,空(null)的ascii码是0
    https://zhidao.baidu.com/question/558202137825309252.html
  2. ord 函数接受一个字符

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

猜你喜欢

转载自blog.csdn.net/qq_40803626/article/details/93405170