Concepts / Comparison Operators and Boolean Logic

Comparison Operators and Boolean Logic

The equality operator == compares two values.

  • Programming

A String That Reads Both Ways

A palindrome is a text that reads the same forwards and backwards. To identify one programmatically, keep the original string, create a reversed version, and compare the two values. If they are equal, the text is a palindrome; if they are different, it is not.

What do you think happens?

What result should a comparison produce when the original string is racecar and its reversed version is also racecar?

  • True

  • False

Reveal answer
Answer:

True

The original and reversed strings are character-for-character identical, so the equality comparison returns True.

Walking Backward Through Characters

Python can reverse a string with the slice text[::-1]. The negative step tells Python to move backward through the string. With a five-character string, Python begins at index 4, the last character, then visits index 3, index 2, index 1, and index 0. The result is a new string whose characters appear in reverse order.

Index 4eReversed textelppaIndex 3lIndex 2pIndex 1pIndex 0astep -1step -1step -1step -1assemble
How do the string indices map when the slice [::-1] visits characters from the last position to the first?
python
Output (expected)
elppa

Turning Equality into a Boolean

The equality operator == compares two values. When Python compares two strings with ==, it checks whether every character in the same position matches. The result is a Boolean value: True when the strings match and False when they do not.

OriginalracecarReversedracecarTrueall positions match====
How do corresponding characters in the original and reversed strings determine whether the entire string reads the same in both directions?
Original stringReversed stringEquality resultPalindrome?
racecarracecarTrueYes
helloollehFalseNo

The equality result directly identifies whether the original and reversed strings match.

Building the Palindrome Checker

A small palindrome checker can separate the work into two helper functions. The reverse() function creates the reversed string. The is_palindrome() function calls reverse(), compares the reversed result with the original using ==, and returns the Boolean result.

Input textracecarreverse(text)create reversed stringtext == reversedcompare valuesTruepalindromeFalsenot a palindromepass textreturn reversedequaldifferent
What happens next as the program receives the string, creates its reverse, compares the two values, and returns the result?
Code walkthrough
def reverse(text):
    return text[::-1]

def is_palindrome(text):
    reversed_text = reverse(text)
    return text == reversed_text

text = input("Enter text: ")
print(is_palindrome(text))
  1. 1
    The definition of reverse() creates a helper for reversing text.
  2. 2
    The definition of is_palindrome() creates a helper for comparing the original and reversed values.
  3. 3
    When is_palindrome() receives racecar, reverse(text) returns racecar.
  4. 4
    The expression text == reversed_text compares racecar with racecar and evaluates to True.
  5. 5
    The program prints the returned result.
  • reverse() receives a string and returns the string in reverse order.
  • is_palindrome() stores the reversed string in reversed_text.
  • The equality operator compares the original and reversed strings.
  • input() captures the user's text, and print() displays the returned Boolean result.
Output
Enter text: racecar
True
Worked example

Tracing racecar

Determine the result of is_palindrome("racecar").

  1. 1
    Receive the input

    The function receives the original string racecar.

  2. 2
    Reverse the string

    The reverse() helper uses text[::-1]. Because racecar reads the same backward, the reversed value is also racecar.

  3. 3
    Compare the values

    The equality expression compares racecar with racecar. Every character in the same position matches.

  4. 4
    Return the Boolean

    The equality comparison evaluates to True, so the function returns True.

Result

is_palindrome("racecar") returns True.

Tracing a Failing Result

When a palindrome checker produces an unexpected result, inspect the intermediate values instead of looking only at the final Boolean. First verify that the reverse() function actually reverses the text. Next verify that the comparison uses ==. Finally confirm that input() captured the intended text.

InputhelloReversed textollehEqualityhello == ollehFalsedifferent valuesreversecompareevaluate
Which intermediate string values and comparison result does the program produce at each step when the final palindrome result is unexpected?
python
Output (expected)
hello
olleh
False
  • Common mistake

    Forgetting to reverse the text

    What it looks like

    Comparing text with text instead of comparing text with text[::-1].

    Why it fails

    The program is not checking whether the text matches its backward form.

    Do this instead

    Create a reversed value with the negative-step slice before using the equality operator.

  • Common mistake

    Using incorrect slice syntax

    What it looks like

    Writing text[-1] when the goal is to create a reversed string.

    Why it fails

    The negative index selects one character, while text[::-1] traverses the complete string backward.

    Do this instead

    Use text[::-1] to visit every character from the last position to the first.

  • Common mistake

    Using a comparison operator other than ==

    What it looks like

    Replacing the equality comparison with a different operator.

    Why it fails

    The palindrome strategy requires checking whether the two string values are equal.

    Do this instead
    Use text == reversed_text.
  • Common mistake

    Ignoring case sensitivity

    What it looks like

    Assuming text with different letter cases will compare equal.

    Why it fails

    String comparison checks the characters in corresponding positions, including their exact forms.

    Do this instead

    Confirm whether the input's capitalization should be treated as significant.

  • Common mistake

    Debugging only the final Boolean

    What it looks like

    Seeing False without checking the original and reversed strings.

    Why it fails

    The error may be in input capture or in the reverse() function rather than in the final comparison.

    Do this instead

    Inspect the input, the reversed value, and the equality expression in that order.

Practice the Trace

EASY

Trace the following input through a palindrome checker: text = "level". Write down the original string, the result of text[::-1], and the result of text == reversed_text.

Hints
  • Begin at the last character and move backward one position at a time.
  • Compare the original and reversed strings character by character.
MEDIUM

A checker returns False for an input you expected to be a palindrome. List the three intermediate checks you would perform before changing the comparison logic.

Hints
  • Check the captured input.
  • Check the value returned by reverse().
  • Check that the equality operator is being used.

Key Takeaways

  1. 1
    A palindrome reads the same forwards and backwards.
  2. 2
    The slice text[::-1] reverses a string by moving from its last character to its first.
  3. 3
    The equality operator == produces True when the original and reversed strings match and False when they differ.
  4. 4
    A reliable debugging trace checks the input, the reversed string, the comparison operator, and the final Boolean result.
  5. 5
    Case sensitivity can affect whether two strings compare equal.

At a glance

  • A palindrome is identified by comparing a string with its reversed version.
  • Python's negative-step slice text[::-1] visits characters from the last index to the first.
  • The == operator turns the string comparison into the Boolean result True or False.
  • Tracing input, reversed text, and the equality expression helps locate unexpected results.
  • Case sensitivity and incorrect slice or comparison syntax are common sources of mistakes.

Related concepts

More concepts starting with C