Reputation: 51
Please tell me why python choose this:
print(max("NEW new"))
OUTPUT: w
Had i understood correctly: lowercase in python on the first place, on the second place uppercase?
Thanks!
Upvotes: 0
Views: 2342
Reputation: 2323
This is just part of Python's definition for > on strings. It does lexicographic comparison with all lowercase letters coming after all uppercase letters. This is a result of the ordering of ASCII.
Since a string is an interable of characters, and max() goes through its iterable argument one by one and returns the one that compares > all the others, 'w' is the result.
Upvotes: 0
Reputation: 12027
in the ascii code lower case letters have a higher code (I.E come later in the table) then upper case letters. you can see this by printing the ascii code for each letter
for letter in "NEW new":
print(f'{letter} : {ord(letter)}')
OUTPUT
N : 78
E : 69
W : 87
: 32
n : 110
e : 101
w : 119
as you can see lower case w has the highest (max) value.
Upvotes: 0
Reputation: 904
max will compare the ASCII value of each character in the string. You can see for yourself what they are by trying out ord('N')
or ord(' ')
or ord('w')
here is the result from python interpreter
>>> string = "NEW new"
>>> for s in string:
... print(s , "--", ord(s))
...
N -- 78
E -- 69
W -- 87
-- 32
n -- 110
e -- 101
w -- 119
>>>
Upvotes: 3