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.

index 0hposition 0oindex 1eposition 1lindex 2lposition 2lindex 3lposition 3eindex 4oposition 4hfirstnextnextnextlast
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.

Code walkthrough
def reverse(text):
    return text[::-1]

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

word = "racecar"
print(is_palindrome(word))
  1. 1
    The definition of reverse() associates the name with a function that uses text[::-1].
  2. 2
    The definition of is_palindrome() associates another name with a function that compares text with reversed_text.
  3. 3
    word receives the value racecar.
  4. 4
    is_palindrome(word) passes racecar to the checking function.
  5. 5
    reverse(text) produces racecar because racecar reads the same in reverse.
  6. 6
    The comparison racecar == racecar evaluates to True.
  7. 7
    print() displays True.
  • reverse() receives a string and returns its characters in reverse order.
  • is_palindrome() creates the reversed string by calling reverse().
  • The equality operator compares the original and reversed strings.
  • The result of the comparison is returned.
Output
True
Input texttextReverse texttext[::-1]Compare stringstext == reversed_textTruematching valuesFalsedifferent valuespass textreturn reversed textequalnot equal
What happens when the function receives a string, reverses it, compares the two values, and returns the result?

Matching and Mismatching Positions

Worked example

Two Complete Comparisons

Determine the palindrome result for racecar and hello.

  1. 1
    racecar

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

  2. 2
    Comparison

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

  3. 3
    hello

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

  4. 4
    Comparison

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

Result

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

position 0rposition 0rposition 1aposition 1aposition 2cposition 2cposition 3eposition 3eposition 4cposition 4cposition 5aposition 5aposition 6rposition 6rmatchesmatchesmatchesmatchesmatchesmatchesmatches
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.

Inputracecarreverse()racecar==racecar == racecarTruereturned resultpasses textreturns reversed textequal
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
  • Common mistake

    Forgetting to reverse the string before comparing

    What it looks like

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

    Why it fails

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

    Do this instead

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

  • Common mistake

    Using incorrect slice syntax

    What it looks like

    Writing a slice that does not use a negative step for reversal.

    Why it fails

    The characters will not be traversed backward as required.

    Do this instead

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

  • Common mistake

    Using an operator other than ==

    What it looks like

    Replacing the equality comparison with a different operator.

    Why it fails

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

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

    Overlooking case sensitivity

    What it looks like

    Treating differently cased text as automatically identical.

    Why it fails

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

    Do this instead

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

  • Common mistake

    Failing to verify the captured input

    What it looks like

    Assuming the input value is correct without checking what input() captured.

    Why it fails

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

    Do this instead

    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. 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.

Related concepts

More concepts starting with S