Check Whether a String Is a Palindrome

Easy ⏱ 8 min 78% acceptance ★★★★☆ 4.4
Write a function is_palindrome(text) that returns True if text reads the same forwards and backwards (case-insensitive, ignoring spaces), and False otherwise.

Examples

Example 1
Input
is_palindrome("Racecar")
Output
True
Explanation

"racecar" reversed is still "racecar" once case is normalized.

Example 2
Input
is_palindrome("hello")
Output
False
Explanation

"hello" reversed is "olleh", which does not match.

Constraints

  • Comparison must be case-insensitive.
  • Spaces should be ignored.

Topics

Functionsreturn values

Companies

InfosysCapgemini

Hints

Hint 1

Normalize with text.lower().replace(" ", "") before comparing.

Hint 2

A string reversed with slicing is text[::-1].

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.