Free lab Real Python 3. Zero installs. Your code stays in this browser. Open the playground

Palindrome Check

Easy · 35 XP

Write is_palindrome(s) returning True if s reads the same reversed (ignore case). Print the result for "Level" and "Python".

Target output
True
False
Blank · autosaved
PYpalindrome-check.py
Run your code to check it…
Guide

How to approach Palindrome Check

A palindrome reads the same in both directions, so the most direct test is to build the reversed string and compare. In Python the slice s[::-1] walks the string with a step of -1 and returns it reversed.

The prompt says to ignore case, and that is the whole trick: "Level" reversed is "leveL", which is a different string. Normalise first with s.lower(), then compare. Make the function return the boolean rather than print it, so the caller decides what to do with the answer.

Worked through on this data

  1. "Level" lowercases to "level". Reversed it is still "level", so the comparison is True.
  2. "Python" lowercases to "python". Reversed it is "nohtyp", so the comparison is False.
  3. The two prints show True and False, matching the target.

Common mistakes

  • Skipping .lower() makes "Level" fail.
  • Printing inside the function and returning nothing makes the outer print() show None under each answer.
  • Reversing with s.reverse(): strings have no such method, and reversed(s) returns an iterator, not a string, so comparing it to s is always False.

Time and space

Lowercasing, reversing and comparing each walk the string once: O(n) time and O(n) extra space for the copies. A two-pointer version that compares s[i] with s[-1 - i] needs no copy at all.

Where this shows up

The same normalise-then-compare move is how real systems match usernames, email addresses and search terms: decide what differences do not matter, strip them, then compare.

Take it further

Make "A man, a plan, a canal: Panama" return True by keeping only characters where ch.isalnum() is true before comparing.

Your next ten minutes

Write Python that does something useful.

Start free. No install, no card, no passive video marathon.

Start learning free → Explore the path