Palindrome Checker

Published: (December 15, 2025 at 09:55 AM EST)
1 min read
Source: Dev.to

Source: Dev.to

What is a palindrome?

A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization).

Examples

InputResult
racecarTrue
A man a plan a canal PanamaTrue
helloFalse

Python solution

def is_palindrome(s):
    """
    Checks if a string is a palindrome.
    Ignores case and spaces.
    """
    s = s.lower()
    s = s.replace(" ", "")
    return s == s[::-1]

# Test cases
print(is_palindrome("wilabaliw"))               # True
print(is_palindrome("A man a plan a canal Panama"))  # True

How it works

  1. Convert to lowercase

    s = s.lower()

    This ensures that "Racecar" and "racecar" are treated as the same string.

  2. Remove spaces

    s = s.replace(" ", "")

    Spaces are irrelevant for palindrome checking in phrases like "A man a plan...".

  3. Compare with reversed string

    return s == s[::-1]

    s[::-1] creates a reversed copy of the string. If the cleaned original string equals its reverse, the function returns True; otherwise, False.

Happy coding!

Back to Blog

Related posts

Read more »

Flatten a Nested List

Hey everyone! 👋 I know I've been a bit quiet lately. I actually came down with a pretty bad flu last week, which completely knocked me out. 🤒 That's why I mis...