Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Using min() and max() With Strings

00:00 Using min() and max() With Strings and Iterables of Strings. Using min() and max() with numeric values is arguably the most common and useful use case of these functions. However, you can also use the functions with strings and iterables of strings.

00:16 In these cases, the alphabetical order of characters will decide the final results. For example, you can use min() and max() to find the smallest and largest letters in some text.

00:27 In this context, smallest means closest to the beginning of the alphabet, and largest means closest to the end of the alphabet.

00:44 In the first two examples, min() returns 'a', and max() returns 'z', as you’d expect. But take a look at these two examples.

00:55 min() returns 'W', and max() returns 'd'. Why? Because uppercase letters come before lowercase letters in Python’s default character set, UTF-8.

01:11 Python internally treats strings as iterables of characters. So, calling min() or max() with a string as an argument is like calling the function with an iterable of individual characters.

01:23 Using min() or max() with a string as an argument isn’t limited to just letters. You can use strings containing any possible character in your current character set.

01:43 Behind the scenes, they use the character’s numeric value to find the minimum and maximum characters in the input string. For example, in the Unicode character table, the uppercase A has a smaller numeric value than the lowercase a. Python’s built-in ord() function takes a single Unicode character and returns an integer representing the Unicode code point of that character.

02:09 In these examples, the code point for the uppercase "A" is lower than the code point for the lowercase "a". This way, when you call min() and max() with both letters, you get results that match the order of the underlying Unicode code points of the letters.

02:30 Finally, you can also call min() and max() with iterables of strings or with multiple string arguments. Again, both functions will determine their return value by comparing the strings alphabetically.

02:59 To find the smallest or largest string in an iterable of strings, min() and max() compare all the strings alphabetically based on the code points of the initial characters. In the first example, the uppercase "H" comes before "P", "a", and "w" in the Unicode table.

03:16 So, min() immediately concludes that "Hello" is the smallest string. In the second example, the lowercase "w" comes after all the other strings’ initial letters. But note, there are two words that start with "w", "welcome" and "world". So, Python proceeds to look at the second letter of each word, and the result is that max() returns "world" because "o" comes after "e".

03:42 In the next section of the course, you’ll see min() and max() in operation with another Python data type, dictionaries.

Become a Member to join the conversation.