Concepts / Understanding Python Strings and Characters

Understanding Python Strings and Characters

max() and min() are built-in functions that find the largest and smallest values in any collection.

  • Programming

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.

comes beforecomes beforespaceuppercaseA, B, Clowercasea, b, c
Which character comes first or last when Python compares the characters in a string?

What do you think happens?

For the string Hello world, which character should be the largest, and which should be the smallest?

  • Largest: H; smallest: w
  • Largest: w; smallest: the space
  • Largest: the space; smallest: H
Reveal answer

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").

List the characters: The string contains a space, the uppercase character H, and lowercase characters including e, l, o, w, r, and d.

Apply 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.

Compare lowercase characters: Among the lowercase characters present, w comes later in the standard ordering than the other lowercase characters in this string.

Return one value: max() returns the single character w, while min() 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.

compare valuesmax() returnsmin() returns3, 8, 2, 6numeric collectionnumeric comparisonnumber line ordering8maximum2minimum
How does a collection operation move from several numbers to one largest or smallest value?

Finding extremes in a number list

Determine the values returned by max([3, 8, 2, 6]) and min([3, 8, 2, 6]).

Compare numeric values: Python compares the numbers by their positions on the number line.

Find the largest: 8 is larger than 3, 2, and 6, so it is the maximum.

Find 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

CollectionWhat Python comparesWhat max() returnsWhat min() returns
Numeric listNumeric valuesThe largest numberThe smallest number
StringCharacters in standard orderingThe character ordered lastThe character ordered first
numeric orderingcharacter ordering[3, 8, 2, 6]numbers8 or 2one numberHello worldcharactersw or spaceone character
What changes when max() and min() receive numbers instead of characters?

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

  • Expecting max() or min() to return a new collection

    Each function returns a single value from the collection.

    Fix: Expect one number from a numeric collection or one character from a string.

  • Assuming character ordering is ordinary alphabetical order

    Python's standard ordering places spaces before uppercase letters, then lowercase letters.

    Fix: Check the character categories before predicting the result.

  • Forgetting that an empty collection has no value to select

    There is no largest or smallest member when the collection is empty.

    Fix: Check that the collection contains a value before applying these functions.

Practice Check

MEDIUM

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, and min() returns the smallest.
  2. Both functions return one value rather than a new collection.
  3. Numbers are compared by numeric value.
  4. String characters follow Python's standard ordering: spaces, uppercase letters, then lowercase letters.
  5. An empty collection has no member that can serve as its maximum or minimum.

Key Takeaways

  • max() and min() 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.