Concepts / Unicode and character encoding

Unicode and character encoding

Comparison operators work on strings to check equality and determine alphabetical order.

  • Programming

A Comparison Begins at the First Character

When Python compares two strings, it does not treat each string as an indivisible whole. It examines their characters from left to right. The first pair of different characters determines the result. If the characters at one position match, Python continues to the next position.

What do you think happens?

What is the result of comparing 'apple' and 'banana' with the less-than operator?

  • True
  • False
  • The comparison cannot be decided
Reveal answer

Answer: True

Python compares the first characters, 'a' and 'b'. Because 'a' has a lower Unicode value than 'b', the comparison is decided at position 0. The remaining characters do not need to be examined.

compare position 0'a' is lower than 'b'aposition 0bposition 0True'apple' < 'banana'pposition 1aposition 1
How does Python compare two strings one character at a time, and what happens when it finds the first differing character?

Operators for Equality and Order

The equality operator, ==, checks whether two strings are exactly the same. The comparison returns True only when both strings contain the same characters in the same order. The less-than operator, <, returns True when the first string comes before the second alphabetically. The greater-than operator, >, returns True when the first string comes after the second alphabetically.

OperatorQuestion it asksTrue when
==Are the strings exactly the same?Both strings have the same characters in the same order
<Does the first string come before the second?The first string comes before the second alphabetically
>Does the first string come after the second?The first string comes after the second alphabetically

The three string comparison operators described in the source material.

Choosing the correct operator

Compare a string with the reference string 'banana'. Decide which operator expresses each question.

Check identity: Use == when the question is whether the string is exactly 'banana', with the same characters in the same order.

Check earlier order: Use < when the question is whether the string comes before 'banana' alphabetically.

Check later order: Use > when the question is whether the string comes after 'banana' alphabetically.

The operator must match the question: == tests exact equality, < tests earlier alphabetical order, and > tests later alphabetical order.

Tracing Unicode Values from Left to Right

Python uses Unicode values when comparing characters. For 'apple' and 'banana', it compares 'a' with 'b' at position 0. Because those characters differ, the comparison is decided immediately. If two strings begin with the same character, Python moves to the next position. For example, 'apple' and 'apricot' share their first character, so the comparison continues to the next character rather than being decided at the beginning.

compare position 0compare position 1characters matchcharacters matchalower Unicode valueasame Unicode value atposition 0next positioncontinue when charactersmatchpnext position if neededpsame Unicode value atposition 1
How does each character correspond to a Unicode value that Python can use during comparison?

Lexicographical comparison is sequential: compare position 0 first, then position 1 only if necessary, and continue until a difference determines the result.

Case Changes the Result

String comparison is case-sensitive. An uppercase letter and its lowercase version are treated as different characters because they have different Unicode values. Therefore, 'Apple' and 'apple' are not equal. The source also states that 'Apple' < 'apple' is True because uppercase letters have lower Unicode values than lowercase letters.

different first charactersdifferent first characterslower Unicode value at position 0higher Unicode value at position 0Appleuppercase AFalse'Apple' == 'apple'applelowercase aTrue'Apple' < 'apple'
Why can uppercase and lowercase versions of the same letter produce different comparison results?

Debugging an Unexpected Comparison

When a comparison produces an unexpected result, trace both strings from left to right. Check whether the characters at position 0 match. If they do, inspect position 1, then the next position. The first mismatch is the most important evidence because it determines the comparison. Also check capitalization at that position.

begininspect charactersyesnodeciderepeatTwo stringsCompare a positionleft to rightCharacters matchNext positioncontinue the traceComparison result==, <, or >First differenceUnicode values decide
Which character position causes two strings to compare as unequal or appear in an unexpected order?
  • Treating equality as a loose match

    The strings do not contain the same characters: their first characters differ in case.

    Fix: Remember that == requires the same characters in the same order, and string comparison is case-sensitive.

  • Reading every character before deciding the order

    The first characters already differ, so their Unicode values decide the result.

    Fix: Stop at the first differing character when tracing a comparison.

  • Using the wrong relational direction

    The < operator checks whether the first string comes before the second; > checks whether it comes after.

    Fix: Translate the question into before, after, or exactly equal before choosing the operator.

For a failed comparison, write the two strings in aligned positions and identify the first mismatch. Record whether the mismatch is caused by a different letter or by uppercase versus lowercase. This gives you a concrete reason for the result instead of treating the comparison as unpredictable.

Applying the Three-Way Check

A comparison can be organized into three cases: the string comes before the reference string, comes after it, or equals it. The source describes a structure using <, >, and an else branch for equality. The equality case is reached after the string is neither less than nor greater than the reference string.

MEDIUM

For each pair, identify the first position that decides the result and choose the correct conclusion: equal, first string comes before, or first string comes after. Pair 1: 'apple' and 'banana'. Pair 2: 'apple' and 'apricot'. Pair 3: 'Apple' and 'apple'.

Hints
  • Begin at position 0 for every pair.
  • If the characters match, move to the next position.
  • Check case carefully in Pair 3.

Tracing a comparison

Determine the result of comparing 'apple' and 'apricot' in alphabetical order.

Position 0: Both strings begin with 'a', so the comparison is not decided yet.

Position 1: Both strings have 'p' at position 1, so continue.

Position 2: The strings differ: 'p' in 'apple' is compared with 'r' in 'apricot'. This is the first difference, so it determines the order.

'apple' comes before 'apricot' because the first differing character in 'apple' has a lower Unicode value than the corresponding character in 'apricot'.

What to Remember

  1. Use == to test whether two strings have exactly the same characters in the same order.
  2. Use < and > to determine whether one string comes before or after another alphabetically.
  3. Python compares strings lexicographically from left to right and stops when the first differing character decides the result.
  4. Unicode values determine how corresponding characters compare.
  5. String comparison is case-sensitive, so uppercase and lowercase letters can produce different equality and ordering results.

Key Takeaways

  • String equality requires identical characters in identical order.
  • Relational operators compare strings alphabetically using lexicographical order.
  • Python checks characters from left to right and uses the first difference to decide the result.
  • Unicode values and case affect the comparison, so uppercase and lowercase versions of a letter are distinct.
  • To debug a comparison, trace both strings position by position and inspect the first mismatch.