Max() and min() on a series of strings


How do the max() and min() functions work with a series of string arguments?

When running this code:

max_string = max("hi","hello","salutations")
print max_string

I got an output of ‘salutations’ and so guessed it had something to do with character length.

But when running this code:

min_string = min("hi","hello","salutations")
print min_string

I got an output of ‘hello’.

Can someone please explain how this works ?


Not length, highest ordinal. ‘s’ => 115, vs. ‘h’ => 104.

he has a lower ordinal (‘e’ => 101) than hi (‘i’ => 105).


Hi mtf! Thanks for the reply. I’m still a bit confused though, what exactly do you mean by ‘ordinal’?


The ASCII code of each character is its ordinal.

A .. Z  =>  65 .. 90
a .. z  =>  97 .. 122

In Python we can find the ordinal using the ord() function with a string argument.

>>> ord('a')

and we can determine the character from the ordinal using the chr() function.

>>> chr(97)


Ah that makes much more sense now. Thanks mtf!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.