Comparing Strings: Equality and Ordering
For a case-sensitive comparison, do not rely only on alphabetical intuition. Write down the ASCII values of the characters that determine the comparison. This makes the result predictable.
Try it: Comparing Strings: Equality and Ordering
How Python compares two strings: == needs the same characters in the same order (capitalisation and spaces count), while <, >, <= and >= walk both strings from the left and let the first differing character decide by its code point — so every uppercase letter sorts before every lowercase one — with a prefix coming before the longer string, and .lower() on both sides making the comparison case-insensitive.
How it works
- == and != first compare lengths (different lengths are never equal), then the characters; case and spacing are part of the characters.
- <, <=, >, >= compare position 0 of both strings, then 1, 2, …; equal characters move on to the next position.
- The FIRST position whose characters differ decides, by their code points (ord): 'A'–'Z' are 65–90 and 'a'–'z' are 97–122, so 'Pineapple' < 'banana' and 'Z' < 'a'. Later characters are never looked at.
- If one string runs out first it is a prefix of the other, and the shorter string is smaller ('app' < 'apple'); with no difference at all the strings are equal.
- Calling .lower() on both strings before comparing makes capitalisation irrelevant (for equality and for ordering, e.g. sorted(words, key=str.lower)).
Default run (3 steps): Evaluate 'Pineapple' < 'banana'. Ordering compares the strings character by character, by code point (ord), from the left. … So 'Pineapple' < 'banana' is True. sorted([a, b]) would put 'Pineapple' first.
Simplified: Two strings of up to 16 characters (printable ASCII and a few Latin letters, all below U+0100, so code point and alphabet order are the same thing here). Results, .lower() and sorted() order match CPython 3.12 on 900+ random pairs.
Loading the simulation…
Why Human Reading Misleads
When people read Python and python, we recognize the same word with different capitalization. Python does not automatically make that human interpretation. During string comparison, uppercase and lowercase letters are treated as different characters because they have different ASCII numeric values. This can make Python's ordering differ from the alphabetical order you expect.
What do you think happens?
Which string comes first in Python's ordering: Pineapple or banana?
Reveal answer
Pineapple
Python compares the first characters, P and b. Uppercase letters have lower ASCII values than lowercase letters, so uppercase P comes before lowercase b.
ASCII Values Set the Order
ASCII assigns numeric values to characters. The uppercase letters range from ASCII 65 through 90, while the lowercase letters range from ASCII 97 through 122. Because every uppercase letter has a lower ASCII value than every lowercase letter, all uppercase letters come before all lowercase letters in Python's string ordering. That is why Z comes before a, even though a human reader usually places a before Z in alphabetical order.
For a case-sensitive comparison, do not rely only on alphabetical intuition. Write down the ASCII values of the characters that determine the comparison. This makes the result predictable.
Tracing the First Characters
Pineapple and banana
Determine which string comes first in Python's ordering.
- 1Inspect the first characters
The first character of Pineapple is uppercase P. The first character of banana is lowercase b.
- 2Compare their character values
Uppercase P belongs to the uppercase ASCII range, which comes before the lowercase ASCII range containing b.
- 3Apply the ordering
Because P comes before b under Python's case-sensitive ordering, Pineapple comes before banana.
Pineapple comes before banana in Python's ordering, even though alphabetical intuition might place banana first.
In this comparison, later characters do not change the result because the first characters already determine the ordering. The important trace is P versus b: the uppercase character is ordered before the lowercase character.
Equality and Ordering
| Question | What case-sensitive comparison means | Source-grounded example |
|---|---|---|
| Are the strings equal? | Uppercase and lowercase versions are different characters, so capitalization can make strings different. | Python and python are not automatically treated as equivalent. |
| Which string comes first? | The character values determine the ordering, so uppercase letters come before lowercase letters. | Pineapple comes before banana. |
Equality asks whether two strings match; ordering asks which string is placed first.
Equality and ordering are related but answer different questions. Equality checks whether two strings should be treated as the same sequence of characters. Ordering determines which string comes before the other according to the character values. In both cases, Python's default behavior is case-sensitive: uppercase and lowercase forms are different characters.
Standardizing Before Comparison
Compare the original strings directly: Python and python remain different because their capitalization differs.
Case differences remain part of the comparison.
Convert both strings to the same case first: Python becomes python python remains python The standardized forms can then be compared as equivalent.
Both strings use the same case before comparison.
For a case-insensitive comparison, convert both strings to the same case before testing equality or ordering. Lowercase is the most common choice, although converting both strings to uppercase works as well. With this approach, Python, python, and PYTHON are treated as equivalent forms for the comparison.
Use standardization when comparing user input or data collected from different sources and capitalization should not affect the result. Keep the original strings available if their original capitalization still matters elsewhere.
Mistakes to Avoid
Common mistake Assuming Python uses ordinary alphabetical order
What it looks likeExpecting banana to come before Pineapple
Why it failsPython's comparison is affected by ASCII values. Uppercase letters come before lowercase letters.
Do this insteadCheck the case and compare the relevant character values before predicting the result.
Common mistake Assuming different capitalization makes strings equivalent
What it looks likeTreating Python and python as automatically equal
Why it failsPython treats uppercase and lowercase letters as different characters during string comparison.
Do this insteadConvert both strings to the same case first when the comparison should ignore capitalization.
Common mistake Standardizing every comparison
What it looks likeIgnoring case when comparing a password
Why it failsSome data uses capitalization intentionally, so changing the comparison rule can remove meaningful differences.
Do this insteadPreserve case-sensitive comparison when case carries meaning.
Check Your Prediction
For each pair, predict which string comes first under Python's case-sensitive ordering. Explain your prediction by identifying the first characters and their case: Pineapple versus banana; Z versus a; Python versus python.
Hints
- Compare the first characters before considering the rest of either string.
- Remember that uppercase ASCII values range from 65 to 90 and lowercase ASCII values range from 97 to 122.
- If the strings differ only by capitalization, do not assume they are equivalent unless both have been standardized to the same case.
A reliable method is to trace the comparison in three steps: identify the first characters being compared, determine whether their cases differ, and use the ASCII ranges to predict the result. If case should not matter, standardize both strings before applying the comparison.
Key Takeaways
- 1Python treats uppercase and lowercase letters as different characters because their ASCII numeric values differ.
- 2Uppercase letters have ASCII values 65 through 90, while lowercase letters have values 97 through 122, so uppercase letters come first in Python's ordering.
- 3Pineapple comes before banana because the first characters are uppercase P and lowercase b.
- 4Equality and ordering answer different questions, but both are affected by case-sensitive comparison.
- 5For case-insensitive comparisons, convert both strings to the same case before comparing, unless capitalization carries meaning.
At a glance
- ASCII values, not human alphabetical intuition, determine case-sensitive string ordering in Python.
- All uppercase letters come before all lowercase letters, so mixed-case strings can produce surprising ordering results.
- The first differing character can determine the ordering, as shown by Pineapple and banana.
- Convert both strings to the same case when capitalization should not affect equality or ordering.
- Keep case sensitivity when capitalization carries meaning, such as in passwords or other case-sensitive data.