String Manipulation in Python
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 Backward
A palindrome is a text that reads the same forwards and backwards. A program can identify one by creating a reversed version of the original string and comparing the two strings. If they match, the text is a palindrome; if they do not, it is not.
What do you think happens?
What should the palindrome comparison return for the string racecar?
Reveal answer
True
The reversed version of racecar is racecar, so every character in the original string matches the character in the same position of the reversed string.
Following Characters Backward
The slice text[::-1] reverses a string. The negative step tells Python to move backward through the characters. For a five-character string, Python starts at index 4, the last character, then visits index 3, index 2, index 1, and index 0. The characters collected in that order form a new string in reverse order.
ollehBuilding the Palindrome Check
The checking strategy has two operations: create a reversed version of the input text, then compare that version with the original. Python's equality operator == checks whether the strings match character for character in the same positions. A match produces True; a mismatch produces False.
def reverse(text):
return text[::-1]
def is_palindrome(text):
reversed_text = reverse(text)
return text == reversed_text
word = "racecar"
print(is_palindrome(word))- 1The definition of
reverse()associates the name with a function that uses text[::-1]. - 2The definition of
is_palindrome()associates another name with a function that compares text withreversed_text. - 3word receives the value racecar.
- 4
is_palindrome(word) passes racecar to the checking function. - 5
reverse(text)produces racecar because racecar reads the same in reverse. - 6The comparison racecar == racecar evaluates to True.
- 7
print()displays True.
reverse()receives a string and returns its characters in reverse order.is_palindrome()creates the reversed string by callingreverse().- The equality operator compares the original and reversed strings.
- The result of the comparison is returned.
TrueMatching and Mismatching Positions
Two Complete Comparisons
Determine the palindrome result for racecar and hello.
- 1racecar
The original string is racecar. Reversing it also produces racecar.
- 2Comparison
racecar == racecar is True because every character in the same position matches.
- 3hello
The original string is hello, while its reversed version is olleh.
- 4Comparison
hello == olleh is False because the strings are different.
racecar is identified as a palindrome, while hello is not.
Tracing an Unexpected Result
When a palindrome checker gives an unexpected result, inspect the execution in order instead of guessing. First verify the value captured as the input. Next verify that reverse() actually produces the characters in reverse order. Then verify that the comparison uses the equality operator ==. Finally check the returned Boolean result.
original: hello
reversed: olleh
comparison: False
FalseCommon mistake Forgetting to reverse the string before comparing
What it looks likeComparing text with text instead of comparing text with text[::-1].
Why it failsThe program has not created the reversed version needed to test the palindrome property.
Do this insteadCreate reversed_text with the negative-step slice and compare text == reversed_text.
Common mistake Using incorrect slice syntax
What it looks likeWriting a slice that does not use a negative step for reversal.
Why it failsThe characters will not be traversed backward as required.
Do this insteadUse text[::-1] to move from the last character toward the first.
Common mistake Using an operator other than ==
What it looks likeReplacing the equality comparison with a different operator.
Why it failsThe palindrome check depends on testing whether the original and reversed strings are equal.
Do this insteadUse text == reversed_text.Common mistake Overlooking case sensitivity
What it looks likeTreating differently cased text as automatically identical.
Why it failsThe source identifies case sensitivity as a common issue in palindrome checks.
Do this insteadWhen debugging, check the exact characters and their case in the captured input.
Common mistake Failing to verify the captured input
What it looks likeAssuming the input value is correct without checking what input() captured.
Why it failsAn unexpected input value can make a correct palindrome function appear to fail.
Do this insteadInspect the input value before tracing the reverse and comparison steps.
Practice the Trace
Write a function named is_palindrome(text) that creates a reversed version with text[::-1], compares the two strings with ==, and returns the result. Test it with racecar and hello. Before running the program, write down the original string, the reversed string, and the expected Boolean result for each input.
Hints
- Create
reversed_textinside the function. - Use return text ==
reversed_text. - For each test, check whether the original and reversed values are identical.
- 1A palindrome reads the same forwards and backwards. Python reverses a string with the negative-step slice text[::-1], which visits characters from the last index to the first. A palindrome function can compare the original and reversed strings with == and return the resulting Boolean value. When debugging, inspect the captured input, the reversed value, the comparison, and the returned result in that order.
Key Takeaways
- A palindrome is text that reads the same forwards and backwards.
- The slice text[::-1] reverses a string by moving backward from its last character.
- The equality operator == determines whether the original and reversed strings match.
- A useful debugging trace checks the input, reversed string, comparison, and returned result.
- For reliable results, check the slice syntax, comparison operator, captured input, and character case.