Concepts / String Manipulation in Python

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.

  • Programming

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?

  • True
  • False
Reveal answer

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.

firstnextnextnextlastindex 0hposition 0oindex 1eposition 1lindex 2lposition 2lindex 3lposition 3eindex 4oposition 4h
How does Python's [::-1] slicing move from the last character to the first, and what character order does it produce?
python
Output
olleh

Building 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))

pass textreturn reversed textequalnot equalInput texttextReverse texttext[::-1]Compare stringstext == reversed_textTruematching valuesFalsedifferent values
What happens when the function receives a string, reverses it, compares the two values, and returns the result?

Matching and Mismatching Positions

Two Complete Comparisons

Determine the palindrome result for racecar and hello.

racecar: The original string is racecar. Reversing it also produces racecar.

Comparison: racecar == racecar is True because every character in the same position matches.

hello: The original string is hello, while its reversed version is olleh.

Comparison: hello == olleh is False because the strings are different.

racecar is identified as a palindrome, while hello is not.

matchesmatchesmatchesmatchesmatchesmatchesmatchesposition 0rposition 0rposition 1aposition 1aposition 2cposition 2cposition 3eposition 3eposition 4cposition 4cposition 5aposition 5aposition 6rposition 6r
How do the original string's character positions correspond to the positions in its reversed version, and when are all characters equal?

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.

passes textreturns reversed textequalInputracecarreverse()racecar==racecar == racecarTruereturned result
What values does the original string, reversed string, comparison, and returned result have at each step of execution?
python
Output
original: hello
reversed: olleh
comparison: False
False
  • Forgetting to reverse the string before comparing

    The program has not created the reversed version needed to test the palindrome property.

    Fix: Create reversed_text with the negative-step slice and compare text == reversed_text.

  • Using incorrect slice syntax

    The characters will not be traversed backward as required.

    Fix: Use text[::-1] to move from the last character toward the first.

  • Using an operator other than ==

    The palindrome check depends on testing whether the original and reversed strings are equal.

    Fix: Use text == reversed_text.

  • Overlooking case sensitivity

    The source identifies case sensitivity as a common issue in palindrome checks.

    Fix: When debugging, check the exact characters and their case in the captured input.

  • Failing to verify the captured input

    An unexpected input value can make a correct palindrome function appear to fail.

    Fix: Inspect the input value before tracing the reverse and comparison steps.

Practice the Trace

EASY

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_text inside the function.
  • Use return text == reversed_text.
  • For each test, check whether the original and reversed values are identical.
  1. A 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.