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


#1

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 ?


#2

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

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


#3

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


#4

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')
97

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

>>> chr(97)
'a'

#5

Ah that makes much more sense now. Thanks mtf!


#6

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