python字符串比较大小

理解原理和相关知识

  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

你可能感兴趣的:(Python,python,字符串,比较大小)