Comparison Operators and Boolean Logic
A palindrome is a text that reads the same forwards and backwards, identified by comparing the original string to its reversed version.
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
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 |
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))
Tracing racecar
Determine the result of is_palindrome("racecar").
Receive the input: The function receives the original string racecar.
Reverse the string: The reverse() helper uses text[::-1]. Because racecar reads the same backward, the reversed value is also racecar.
Compare the values: The equality expression compares racecar with racecar. Every character in the same position matches.
Return 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
FalseForgetting to reverse the text
The program is not checking whether the text matches its backward form.
Fix:
Create a reversed value with the negative-step slice before using the equality operator.Using incorrect slice syntax
The negative index selects one character, while text[::-1] traverses the complete string backward.
Fix:
Use text[::-1] to visit every character from the last position to the first.Using a comparison operator other than ==
The palindrome strategy requires checking whether the two string values are equal.
Fix:
Use text == reversed_text.Ignoring case sensitivity
String comparison checks the characters in corresponding positions, including their exact forms.
Fix:
Confirm whether the input's capitalization should be treated as significant.Debugging only the final Boolean
The error may be in input capture or in the reverse() function rather than in the final comparison.
Fix:
Inspect 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
- A palindrome reads the same forwards and backwards.
- The slice text[::-1] reverses a string by moving from its last character to its first.
- The equality operator == produces True when the original and reversed strings match and False when they differ.
- A reliable debugging trace checks the input, the reversed string, the comparison operator, and the final Boolean result.
- Case sensitivity can affect whether two strings compare equal.
Key Takeaways
- 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.