🔄

String Reversal and Palindrome Check

Jul 9, 2025

Overview

This lecture explains how to reverse a string in Python and create a simple program to check if a user's input is a palindrome.

Reversing a String in Python

  • Python does not have a built-in string reverse method.
  • String slicing extracts characters by specifying start, finish, and step inside square brackets.
  • Leaving start and finish blank with step -1 (i.e., [::-1]) reverses the string.

Palindrome Program Steps

  • Prompt the user to input a string and store it in a variable (e.g., original).
  • Reverse the string using slicing and store it in another variable (e.g., reverse).
  • Use an if statement to compare the original and reversed strings.
  • If both strings are equal, print that the string is a palindrome; otherwise, print that it is not.

Program Example

  • User input is taken and reversed.
  • Conditional logic checks for palindrome status.
  • Program can be run multiple times for different inputs.

Recap of the Process

  • Obtain user input as the original string.
  • Reverse the string using slicing.
  • Use a conditional statement to compare and print the result.

Key Terms & Definitions

  • Palindrome — a sequence of characters that reads the same forwards and backwards.
  • Slicing — extracting parts of a string using start, finish, and step parameters: [start:finish:step].

Action Items / Next Steps

  • Practice writing the palindrome checker program using string slicing and if statements.
  • Test the program with various inputs to confirm correctness.