Palindrome Program in Python
Following Python code will check if a string is palindrome or not.
- Define string to check for palindrome.
- Create a function that will take input string for checking.
- In the function, first reverse the string and then check by comparison whether original string and the reversed string is the same.
- If both the strings are same then print “It is palindrome” else print “It is not palindrome”.
How Does Palindrome Program Works?
A word, phrase, or sequence is said to be palindrome if, it reads the same backwards as forwards, e.g. “madam” or “nurses run”. A number is also a palindrome number if it reads the same backwards or forwards, e.g. “16461”.
To check for a palindrome string, a function is defined that will take a string as an input. The inputted string is then reversed, that is, it is stored in backwards spelling as the reversed string. After that, list() is used to list the characters in a string. Both the input string and the reversed string are converted to lowercase then list() is used to check if the sequence of characters are same in both the string. If yes, then a message will be printed saying “It is palindrome” else a message will be printed saying “It is not palindrome”.
Python Program To Check if a string is Palindrome or not:Python Palindrome ProgramPython1234567891011# change this value for a different outputstr = 'aIbohPhoBiA'def palindrome_or_not(str):rev_str = reversed(str.lower())if list(str.lower()) == list(rev_str):print("It is palindrome")else:print("It is not palindrome")palindrome_or_not(str)
Output for the above Program execution is:
It is palindrome
Instead of hard coding the values, the code can also be modified to get input from the user during program execution. Refer here to know how to get user input from Python code execution.
Previous Post: Python – Random Number Generator Code
Next Post: Excel VBA – Create Defined Name Formula & How to Use
Python Program To Check Palindrome
- Games VCF Countif Record Macro Timer in Excel Task Manager print screen HTML Table Import Data Thumbnails Text to Speech TTS Candy Crush Block Apps in FaceBook C++ Programing Machine learning AI bulk email Excel VBA vba color index vba color codes Android social media Twitter Google+ Google Adsense Tips bitcoin Calendar in Excel outlook vba mass email Data Mining Excel Macro Facebook WhatsApp python Wordpress