Comparison Operators and Boolean Logic
The equality operator == compares two values.
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?
Reveal 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.
elppaTurning 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.
| Original string | Reversed string | Equality result | Palindrome? |
|---|---|---|---|
| racecar | racecar | True | Yes |
| hello | olleh | False | No |
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.
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))- 1The definition of
reverse()creates a helper for reversing text. - 2The definition of
is_palindrome()creates a helper for comparing the original and reversed values. - 3When
is_palindrome()receives racecar,reverse(text)returns racecar. - 4The expression text ==
reversed_textcompares racecar with racecar and evaluates to True. - 5The program prints the returned result.
reverse()receives a string and returns the string in reverse order.is_palindrome()stores the reversed string inreversed_text.- The equality operator compares the original and reversed strings.
input()captures the user's text, andprint()displays the returned Boolean result.
Enter text: racecar
TrueTracing racecar
Determine the result of is_palindrome("racecar").
- 1Receive the input
The function receives the original string racecar.
- 2Reverse the string
The
reverse()helper uses text[::-1]. Because racecar reads the same backward, the reversed value is also racecar. - 3Compare the values
The equality expression compares racecar with racecar. Every character in the same position matches.
- 4Return the Boolean
The equality comparison evaluates to True, so the function returns True.
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.
hello
olleh
FalseCommon mistake Forgetting to reverse the text
What it looks likeComparing text with text instead of comparing text with text[::-1].
Why it failsThe program is not checking whether the text matches its backward form.
Do this insteadCreate a reversed value with the negative-step slice before using the equality operator.
Common mistake Using incorrect slice syntax
What it looks likeWriting text[-1] when the goal is to create a reversed string.
Why it failsThe negative index selects one character, while text[::-1] traverses the complete string backward.
Do this insteadUse text[::-1] to visit every character from the last position to the first.
Common mistake Using a comparison operator other than ==
What it looks likeReplacing the equality comparison with a different operator.
Why it failsThe palindrome strategy requires checking whether the two string values are equal.
Do this insteadUse text == reversed_text.Common mistake Ignoring case sensitivity
What it looks likeAssuming text with different letter cases will compare equal.
Why it failsString comparison checks the characters in corresponding positions, including their exact forms.
Do this insteadConfirm whether the input's capitalization should be treated as significant.
Common mistake Debugging only the final Boolean
What it looks likeSeeing False without checking the original and reversed strings.
Why it failsThe error may be in input capture or in the
reverse()function rather than in the final comparison.Do this insteadInspect the input, the reversed value, and the equality expression in that order.
Practice the Trace
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.
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
- 1A palindrome reads the same forwards and backwards.
- 2The slice text[::-1] reverses a string by moving from its last character to its first.
- 3The equality operator == produces True when the original and reversed strings match and False when they differ.
- 4A reliable debugging trace checks the input, the reversed string, the comparison operator, and the final Boolean result.
- 5Case 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.