Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Examples
  3. Palindrome Checker

Strings · Python example

Palindrome Checker

Normalize a phrase and compare it with its reverse to detect a palindrome.

Open the compilerAll examples
beginnerstringsslicingpalindrome

A palindrome reads the same forwards and backwards. Real phrases need normalizing: ignore case, spaces, and punctuation first.

example.py
text = "Never odd or even"
cleaned = "".join(char.lower() for char in text if char.isalnum())

if cleaned == cleaned[::-1]:
    print(f'"{text}" is a palindrome')
else:
    print(f'"{text}" is not a palindrome')

How the code works

  • The phrase becomes 'neveroddoreven'.
  • Its reverse is identical.
  • The if branch prints the palindrome message.

Key ideas

  • The generator expression keeps only alphanumeric characters.
  • lower() makes the comparison case-insensitive.
  • [::-1] reverses the cleaned string.

Try changing it

  • Read the phrase from input().
  • Use a two-pointer loop instead of slicing.
  • Return True or False from a reusable function.

Related examples

beginner

Reverse a Number

Reverse the digits of an integer by converting it to a string.

beginner

Count Vowels

Count the vowels in a sentence with a loop and a membership test.

Previous exampleSum of Natural NumbersNext exampleCount Vowels

Expected output

"Never odd or even" is a palindrome

Output is compared after trimming trailing whitespace on each line. Run the code to see it produced by CPython in your browser.

Practice next

Apply the same idea with automatic tests or browse a guided project.

ExercisesProjects