Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 44

Palindrome Program in Python

In this lesson: Check palindrome strings and numbers in Python using slicing and loops — code with output. Copy & run now. In Hindi & English.

What is a palindrome

A palindrome is a word, phrase or number that reads the same forwards and backwards — like "madam", "level" or 121. Checking one in Python is quick, and it is a great way to practise strings and comparisons.

💡 The core idea: a value is a palindrome if it equals its own reverse.

The easy slicing way

word = "madam"
if word == word[::-1]:
    print("Palindrome")
else:
    print("Not a palindrome")
Palindrome

word[::-1] reverses the string. If the original equals its reverse, it is a palindrome. This one line is the simplest method.

Dry run

Dry Run — is "madam" a palindrome? (word == word[::-1])
StepWhatValue
1word"madam"
2word[::-1] (reversed)"madam"
3Compare: word == word[::-1]"madam" == "madam" → True → Palindrome!
💡 Understand it this way

A palindrome reads the same both ways. In Python word[::-1] reverses the string. If forwards and backwards are equal, it is a palindrome. "madam" reversed is still "madam".

Checking with a loop

To see the logic, compare characters from both ends moving inward.

word = "level"
is_pal = True
n = len(word)
for i in range(n // 2):
    if word[i] != word[n - 1 - i]:
        is_pal = False
        break
print("Palindrome" if is_pal else "Not")
Palindrome

We compare the first with the last, second with second-last, and so on — only up to the middle (n // 2). If any pair differs, it is not a palindrome.

Palindrome number

n = 121
if str(n) == str(n)[::-1]:
    print("Palindrome number")
else:
    print("Not")
Palindrome number

The easiest way for numbers is to convert to a string and use the same slice check. 121 reversed is 121, so it is a palindrome.

Common mistakes

  • Ignoring case for phrases. "Madam" vs "madaM" — lowercase first with .lower().
  • Not removing spaces in phrases. Strip spaces before comparing "nurses run".
  • Looping the whole length. You only need to check up to n // 2.
  • Comparing a number directly with slicing. Convert to a string first: str(n)[::-1].

Summary

  • A palindrome reads the same forwards and backwards.
  • Easiest check: word == word[::-1].
  • A loop compares characters from both ends up to the middle.
  • For numbers, use str(n) == str(n)[::-1].
  • For phrases, lowercase and remove spaces first.

Next, learn reversing in reverse a string or list, or try pattern programs.

Frequently Asked Questions

What is a palindrome?
A palindrome is a word, phrase or number that reads the same forwards and backwards. Examples: "madam", "level", and the number 121. Checking for palindromes is a classic beginner exercise.
What is the easiest way to check a palindrome in Python?
Compare the string to its reverse: word == word[::-1]. If they are equal, it is a palindrome. This is the shortest and most Pythonic method.
How do I check a palindrome without slicing?
Use a loop that compares the first character with the last, the second with the second-last, and so on. If all pairs match, it is a palindrome. This shows the logic that slicing does for you.
How do I check if a number is a palindrome?
Turn it into a string and use the same slice check: str(n) == str(n)[::-1]. Alternatively, reverse the number mathematically and compare it to the original.
Should I ignore case and spaces?
For phrases like "Nurses run", yes — convert to lowercase and remove spaces first, then check. For simple single words or numbers, a direct comparison is enough.
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicPattern Printing Programs Your progress: 44 / 66 (67%) · Lesson 45 / 66 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free