Book a free demo

Easy ยท ages 10+ ยท strings ยท slicing ยท functions

Palindrome checker in Python.

Check whether a word reads the same forwards and backwards.

The program

Press Run to see what it prints. Change anything you like, then run it again.

def is_palindrome(word):
    word = word.lower()
    return word == word[::-1]

for w in ["level", "Python", "Racecar", "noon", "hello"]:
    if is_palindrome(w):
        print(w, "is a palindrome")
    else:
        print(w, "is not a palindrome")
level is a palindrome
Python is not a palindrome
Racecar is a palindrome
noon is a palindrome
hello is not a palindrome

How it works

  1. word[::-1] is a slice that reads the string backwards.
  2. .lower() turns everything into small letters so that 'Racecar' counts.
  3. The function returns True or False, and the loop uses it in an if.
See the Python path โ†’

Try this

  • Add your own words.
  • Ignore spaces so that 'never odd or even' works: use word.replace(" ", "").
  • Find all palindromes in a list of words.

Stuck on an error? Read how to debug Python.

Similar ideas

More programs

All programs โ†’

FAQ

Palindrome checker: questions

What does the palindrome checker program do?

Check whether a word reads the same forwards and backwards. It prints: level is a palindrome / Python is not a palindrome / Racecar is a palindrome โ€ฆ

Which Python ideas does it use?

strings, slicing, functions. Easy level, suitable from about age 10.

How do I run it?

Press Run on this page. To run it on your own computer, install Python 3 and save the code in a file ending in .py. It prints the same thing.

What should I try next?

Add your own words. Ignore spaces so that 'never odd or even' works: use word.replace(" ", ""). Find all palindromes in a list of words.

Start a conversation

Want a teacher to guide the learning?

Tell us the learner's age and what they can already do, and we will suggest a starting point. A parent or guardian can make the enquiry.

  • Tell us the learnerโ€™s age or grade and goals
  • Weโ€™ll suggest a suitable class or learning path
  • Weโ€™ll confirm format, timing and fees with a parent or guardian

For a childโ€™s class, please have a parent or guardian send the enquiry. Avoid sharing passwords or sensitive personal details.

Weโ€™ll use your details only to respond to this request.

Play it, don't just read it

Write some code right now

Free games that need no sign-up. Pick one and play for five minutes.

All free games โ†’
Chat on WhatsApp
Esc

Try