Understanding Python Strings and Characters
A string is treated as a collection of characters for this operation. The result is one character from the original string, not another string containing all characters in sorted order.
One Result from Many Values
Python's max() and min() functions examine the values in a collection and return one value from that collection. max() returns the largest value, while min() returns the smallest value. They do not create a new collection containing the result.
max() finds the largest value in a collection. min() finds the smallest value in a collection.
The kind of values in the collection determines how Python decides what largest and smallest mean. With numbers, Python uses numeric value. With a string, Python compares its characters according to Python's standard character ordering.
Character Ordering in a String
When max() or min() receives a string, Python compares the characters in that string. The standard ordering places spaces before uppercase letters, and uppercase letters before lowercase letters. Therefore, character comparison does not necessarily match the order people expect from ordinary alphabetical sorting.
What do you think happens?
For the string Hello world, which character should be the largest, and which should be the smallest?
Reveal answer
Largest: w; smallest: the space
The string contains a space, uppercase H, lowercase letters, and other lowercase characters. A space comes before uppercase letters, and uppercase letters come before lowercase letters. Among the lowercase letters present, w is the largest character in the standard ordering.
Tracing a String Example
Finding the extremes in Hello world
Determine the values returned by max("Hello world") and min("Hello world").
- 1List the characters
The string contains a space, the uppercase character H, and lowercase characters including e, l, o, w, r, and d.
- 2Apply the ordering
The space comes before uppercase letters, and uppercase letters come before lowercase letters. This makes the space the smallest character in the string.
- 3Compare lowercase characters
Among the lowercase characters present, w comes later in the standard ordering than the other lowercase characters in this string.
- 4Return one value
max()returns the single character w, whilemin()returns the single space character.
max("Hello world") returns w, and min("Hello world") returns the space character.
A string is treated as a collection of characters for this operation. The result is one character from the original string, not another string containing all characters in sorted order.
Numeric Collections
The same functions also work with numeric collections such as lists of numbers. In this case, Python uses numeric value. The largest number is the maximum, and the smallest number is the minimum. This follows the number line and is usually more intuitive than character comparison.
Finding extremes in a number list
Determine the values returned by max([3, 8, 2, 6]) and min([3, 8, 2, 6]).
- 1Compare numeric values
Python compares the numbers by their positions on the number line.
- 2Find the largest
8 is larger than 3, 2, and 6, so it is the maximum.
- 3Find the smallest
2 is smaller than 3, 8, and 6, so it is the minimum.
max([3, 8, 2, 6]) returns 8, and min([3, 8, 2, 6]) returns 2.
Numbers and Characters Compared
| Collection | What Python compares | What max() returns | What min() returns |
|---|---|---|---|
| Numeric list | Numeric values | The largest number | The smallest number |
| String | Characters in standard ordering | The character ordered last | The character ordered first |
Both functions return one value from the collection, but the comparison rule depends on the kind of values being examined.
The function pattern stays the same: give max() or min() a collection and receive one value. What changes is the comparison rule and therefore the type of value selected. A numeric list produces one number, while a string produces one character.
Mistakes Beginners Make
Common mistake Expecting max() or min() to return a new collection
What it looks likeTreating the result as though it contains all values in sorted order
Why it failsEach function returns a single value from the collection.
Do this insteadExpect one number from a numeric collection or one character from a string.
Common mistake Assuming character ordering is ordinary alphabetical order
What it looks likeAssuming a lowercase character must come before an uppercase character because of dictionary-style expectations
Why it failsPython's standard ordering places spaces before uppercase letters, then lowercase letters.
Do this insteadCheck the character categories before predicting the result.
Common mistake Forgetting that an empty collection has no value to select
What it looks likeCalling max() or min() when the collection contains no values
Why it failsThere is no largest or smallest member when the collection is empty.
Do this insteadCheck that the collection contains a value before applying these functions.
Practice Check
For each collection, predict the value returned by max() and min(): [12, 4, 19, 7] and "Cat dog". Explain whether your prediction uses numeric ordering or character ordering.
Hints
- For the number list, compare the values on the number line.
- For the string, look first for the space, then distinguish uppercase and lowercase characters.
- Remember that each function returns one value, not a new collection.
A reliable prediction process is: identify the collection's values, identify the comparison rule, locate the largest or smallest member, and report that single value.
Key Takeaways
- 1
max()returns the largest value in a collection, andmin()returns the smallest. - 2Both functions return one value rather than a new collection.
- 3Numbers are compared by numeric value.
- 4String characters follow Python's standard ordering: spaces, uppercase letters, then lowercase letters.
- 5An empty collection has no member that can serve as its maximum or minimum.
At a glance
max()andmin()select one value from a collection.- Numeric collections use numeric ordering to determine their extremes.
- Strings use character ordering, with spaces before uppercase letters and uppercase letters before lowercase letters.
- The result from a string is one character, not a sorted string.
- Always consider empty collections and the difference between character ordering and ordinary alphabetical expectations.