python-tutorialsmade

Python program to find Palindrome

 

This python example script will help you to find out the entered string is a palindrome or not. The logic is reversing the original string and comparing both the original and reversed string.

Here is the script (palidrome.py):

The code is self explanatory, read the comments in the script.

Steps are:

1. Read the string using python function raw_input() and store it in a variable mystring 
2. Remove space from using replace() function
3. Remove special characters from the string using isalnum() – this function allows only alphabets
4. Change all characters to lower case
5. Now reverse the string using [::-1] and store it in another variable revstring
6. Finally compare both mystring and revstring by == double equal operator

If both the string matches print “Palindrome” or else print “Not Palindrome”


Some palindrome words and sentences to check this script:

1. madam
2. civic
3. A man, a plan, a canal: Panama.
4. No lemon, no melon.

If you are a Python Web developer you may need this CGI script to run on your server:

You can also see the Python web script demo here:

demo

Same program is written in PHP and Java, If you want please have a look:

PHP: http://www.tutorialsmade.com/php-program-find-palindrome/

JAVA: http://www.tutorialsmade.com/java-program-string-palindrome/

 

Related Posts

Leave a Reply