Python Palindrome Checker with isalnum()

Day 3 of my Python & DSA learning journey 🚀 A simple string completely confused me today 🤯 "A man, a plan, a canal: Panama" — is this a palindrome? At first glance I thought — no way, there are too many symbols! But Python solved it in just a few lines. Here's what I learned today 👇 ❓ Valid Palindrome (Ignore Symbols) A palindrome is a word that reads the same forward and backward. Examples: madam racecar But what if the string contains spaces, commas, or symbols? Example: "A man, a plan, a canal: Panama" It is still a palindrome — we just need to ignore the non-alphanumeric characters. 📌 Python provides a very useful function for this: isalnum() → it checks whether a character is a letter or a number. 💻 Example in Python s = "A man, a plan, a canal: Panama" result = "" for ch in s: if ch.isalnum(): result += ch.lower() if result == result[::-1]: print("Palindrome") else: print("Not Palindrome") Output: Palindrome ✅ Here we: • Removed symbols using isalnum() • Converted characters to lowercase using .lower() • Compared the string with its reverse Just a few lines of code — problem solved! 🔥 🔥 Question for developers: Is "race a car" a valid palindrome according to this logic? Drop your answer in the comments and explain why 👇 👀 Day 4 Teaser Tomorrow I’ll share a very interesting string trick used in coding interviews: How to check if two strings are rotations of each other in just one line of Python. Stay tuned 🚀 #Python #DSA #Coding #Programming #LearningInPublic #100DaysOfCode #PythonProgramming

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories