+ 11
How does the following program work? How does max(list) and min(list) work with strings?
6 Respostas
+ 5
lowercase letters are higher than uppercase letters. ord('i') = 105 and ord('P') = 80
+ 10
HonFu Thanks, I get it now!🤗
+ 8
HonFu So, max(list) gives the result as 'is'. Is 'i' > 'p' according to unicode??
+ 5
Yeah, the order is not always what you expect because the decisive factor is the unicode number.
If you want to compare alphabetically and ignore cases, you'd have to lower what you compare, for example 'pYthOn'.lower() == 'PyThoN'.lower().
And if your language has special letters like ä, ö, ü or something, sorting by size can become a bit more complicated.
+ 4
The first two letters of the words are compared. If one is bigger (e. g. 'b' > 'a', unicode being the table to check it), the comparison is already finished.
So 'ba' is bigger than 'ab' because the first letter already decides.
If the letters are equal, comparison moves on to the next pair of letters. And so on.
max and min look for the largest or smallest in the list according to the standard I described.