Summary

Learn how to check if a string is a palindrome using Python. This beginner-friendly guide includes clear explanations, examples, and Python code to understand palindrome checking easily.

Article Body

How to Check If a String Is a Palindrome in Python: Step-by-Step Guide
How to Check If a String Is a Palindrome in Python: Step-by-Step Guide

🧠 What is a Palindrome?

A palindrome is a word, number, phrase, or sequence that reads the same forward and backward.

πŸ” Examples of Palindromes:

  • "madam"

  • "level"

  • "radar"

  • "12321"

  • "racecar"

If you reverse these strings, they will still be the same!


🐍 Python Program to Check for Palindrome

Let’s create a simple Python program that checks whether a given string is a palindrome.

# Program to check if a string is a palindrome

def is_palindrome(string):
    # Step 1: Convert the string to lowercase to ignore case sensitivity
    string = string.lower()

    # Step 2: Remove spaces from the string
    string = string.replace(" ", "")

    # Step 3: Reverse the string
    reversed_string = string[::-1]

    # Step 4: Compare original and reversed strings
    return string == reversed_string

# Example usage
user_input = input("Enter a string to check if it's a palindrome: ")

if is_palindrome(user_input):
    print(f"'{user_input}' is a palindrome!")
else:
    print(f"'{user_input}' is NOT a palindrome.")

🧩 Step-by-Step Explanation

πŸ”Ή Step 1: Convert to Lowercase

string = string.lower()
  • Palindromes are not case-sensitive.

  • So "Madam" should be treated the same as "madam".

πŸ”Ή Step 2: Remove Spaces

string = string.replace(" ", "")
  • Sometimes users input phrases like "A man a plan a canal Panama".

  • To handle such cases, we remove spaces.

πŸ”Ή Step 3: Reverse the String

reversed_string = string[::-1]
  • This is Python’s slicing trick.

  • string[::-1] means: start from the end and go backwards.

πŸ”Ή Step 4: Compare Original and Reversed Strings

return string == reversed_string
  • If both strings match, it means the input is a palindrome.


πŸ§ͺ More Examples

Let’s try some test cases with the function:

print(is_palindrome("Racecar"))       # True
print(is_palindrome("Python"))        # False
print(is_palindrome("A man a plan a canal Panama"))  # True
print(is_palindrome("12321"))         # True

πŸ”„ Bonus: Palindrome Check Without Removing Spaces

If you want to check strictly character-by-character (including spaces), you can skip step 2.


πŸš€ Why Use Python for This?

  • Simple syntax: Easy to read and write

  • Powerful string handling

  • Slicing: A cool Python feature that simplifies reversing strings


🧠 Common Questions

❓ Can a sentence be a palindrome?

Yes! If you ignore spaces and case, sentences like "A man a plan a canal Panama" are palindromes.

❓ What about numbers?

Yes! Numbers like 121 or 12321 are also palindromes.


🎯 Conclusion

Palindrome checking is one of the most common exercises in programming interviews and beginner tutorials. In Python, it becomes incredibly easy with string manipulation and slicing.

With just a few lines of code, you can create a program that checks whether a word, phrase, or number is a palindrome.

Comments

TOPICS MENTIONED IN THIS ARTICLE

About the Author(s)

  • Rahul Kumar photo

    Rahul Kumar

    Software Engineer & Tech Editor

    Rahul is a software engineer and editor at Galaxy Founder, passionate about technology, startups, and digital innovation. With a keen eye for emerging trends and a love for clean, efficient code, Rahul shares insights and resources to help others navigate the evolving tech landscape.

    View all articles by Rahul Kumar

Published by Β· Editorial Policy

Galaxy Founder | Latest News, Job Updates & In-Depth Product Reviews β€” Galaxy Founder brings you the latest news, real-time job postings, and honest product reviews to keep you informed, updated, and ahead. Discover trusted content across trending topics only on Galaxy Founder.