Concepts / String Comparison and Equality in Python

String Comparison and Equality in Python

The program first receives a string from the user.

  • Programming
Interactive lab

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

  1. == and != first compare lengths (different lengths are never equal), then the characters; case and spacing are part of the characters.
  2. <, <=, >, >= compare position 0 of both strings, then 1, 2, …; equal characters move on to the next position.
  3. 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.
  4. 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.
  5. 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.

Educational simulation

Loading the simulation…

A Hidden Response

An Easter Egg is a harmless, hidden feature that programmers add for fun. In this exercise, the hidden feature is a special message that appears when the user enters the exact text na na boo boo. Every other input should continue through the program's normal behavior.

Key point

The special behavior is not a replacement for the normal program. It is an additional path selected only when the user's input matches the intended trigger exactly.

Following the Input

User inputentered textExact match?compare with na na boo booSpecial messageEaster Egg responseNormal behaviorall other inputssubmit textyesno
What happens after the user enters text, and how does the program choose between the special Easter Egg response and normal behavior?

The program first receives a string from the user. It then performs an equality check against the trigger string. If the two strings match, the program selects the Easter Egg response. If they do not match, the program preserves its ordinary behavior. This branching is the role of conditional logic using an if/else structure.

Exact Text Matters

na na boo boorequired triggerSpecial messagematchNa na boo boodifferent capitalizationNormal behaviornot an exact matchna na boo boodifferent spacingboo boo na nadifferent character orderexact equalitynot equalnot equalnot equal
How does Python compare the user's input with the exact string na na boo boo, including spaces and character order?
Definition

String comparison checks whether the user's text matches the trigger text. For this Easter Egg, equality requires the exact sequence na na boo boo. The comparison is case-sensitive and space-sensitive, so capitalization, spaces, and character order all matter.

Worked example

Testing Three Inputs

Determine which behavior should be selected for each input when the Easter Egg trigger is na na boo boo.

  1. 1
    Input 1

    The input is na na boo boo. Its capitalization, spaces, and character order match the trigger, so the special Easter Egg response is selected.

  2. 2
    Input 2

    The input is Na na boo boo. Its capitalization differs from the trigger, so it is not an exact match and the program keeps its normal behavior.

  3. 3
    Input 3

    The input is na na boo boo. Its spacing differs from the trigger, so it is not an exact match and the program keeps its normal behavior.

Result

Only the exact input na na boo boo activates the special response.

Ordering the Check

The Easter Egg check must happen before the program's file-opening logic. This ordering prevents the program from attempting to open a file named na na boo boo when the user has entered the hidden trigger. Once the exact trigger is recognized, the program can respond with the special message instead.

Mistakes with Matching

  • Common mistake

    Treating a similar string as the trigger

    What it looks like

    Using Na na boo boo instead of na na boo boo

    Why it fails

    String comparison is case-sensitive, so the capitalization does not match exactly.

    Do this instead

    Compare the user's input with the exact trigger text.

  • Common mistake

    Ignoring spaces

    What it looks like

    Using na na boo boo with two spaces between the words

    Why it fails

    String comparison is space-sensitive, so different spacing produces a different input.

    Do this instead

    Keep the spaces in the trigger exactly as specified.

  • Common mistake

    Changing the character order

    What it looks like

    Using boo boo na na

    Why it fails

    The characters are not in the same order as the trigger string.

    Do this instead

    Require the exact sequence na na boo boo.

  • Common mistake

    Checking too late

    What it looks like

    Allowing file-opening logic to run before testing for the Easter Egg

    Why it fails

    The program may attempt to open a file using the trigger text as its name.

    Do this instead

    Perform the Easter Egg check before file-opening logic.

Check Your Reasoning

EASY

A user enters a string that looks similar to na na boo boo but has different capitalization or spacing. Decide which branch should run and explain why. Then describe what should happen for an unrelated input.

Hints
  • Compare the input with the trigger character by character.
  • Check capitalization and spaces, not just the general appearance of the words.
  • Any input that is not an exact match should retain normal program behavior.
Key point

A reliable reasoning sequence is: receive the input, compare it with na na boo boo, choose the special response only for equality, and otherwise continue with normal behavior.

Key Takeaways

  • An Easter Egg is a harmless hidden feature added for fun.
  • Conditional if/else behavior can select a special response for one specific input while preserving normal behavior for all other inputs.
  • The trigger is the exact string na na boo boo.
  • String comparison is case-sensitive and space-sensitive, so capitalization, spacing, and character order must match.
  • The Easter Egg check should occur before file-opening logic.

Related concepts

More concepts starting with S