Concepts / Understanding Python Strings and Characters

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.

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

Definition

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.

spaceuppercaseA, B, Clowercasea, b, ccomes beforecomes before
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

Worked example

Finding the extremes in Hello world

Determine the values returned by max("Hello world") and min("Hello world").

  1. 1
    List the characters

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

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

  3. 3
    Compare lowercase characters

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

  4. 4
    Return one value

    max() returns the single character w, while min() returns the single space character.

Result

max("Hello world") returns w, and min("Hello world") returns the space character.

Key point

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.

3, 8, 2, 6numeric collectionnumeric comparisonnumber line ordering8maximum2minimumcompare valuesmax() returnsmin() returns
How does a collection operation move from several numbers to one largest or smallest value?
Worked example

Finding extremes in a number list

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

  1. 1
    Compare numeric values

    Python compares the numbers by their positions on the number line.

  2. 2
    Find the largest

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

  3. 3
    Find the smallest

    2 is smaller than 3, 8, and 6, so it is the minimum.

Result

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

Both functions return one value from the collection, but the comparison rule depends on the kind of values being examined.

[3, 8, 2, 6]numbers8 or 2one numberHello worldcharactersw or spaceone characternumeric orderingcharacter ordering
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

  • Common mistake

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

    What it looks like

    Treating the result as though it contains all values in sorted order

    Why it fails

    Each function returns a single value from the collection.

    Do this instead

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

  • Common mistake

    Assuming character ordering is ordinary alphabetical order

    What it looks like

    Assuming a lowercase character must come before an uppercase character because of dictionary-style expectations

    Why it fails

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

    Do this instead

    Check the character categories before predicting the result.

  • Common mistake

    Forgetting that an empty collection has no value to select

    What it looks like

    Calling max() or min() when the collection contains no values

    Why it fails

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

    Do this instead

    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.
Key point

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

At a glance

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

Related concepts

More concepts starting with U