Introduction to Palindrome? All possible ways in python.
Introduction
A palindrome is a word or phrase that is spelt the same way from back and forth. A palindromic number can be read in both directions. This includes words, as well as numbers and other symbols. In this article, we will introduce the concept of palindromes and demonstrate how to determine if a word or number is a palindrome in Python.
What is a Palindrome?
Any word, phrase or number that reads or is spelt in the same way as back and forth is a palindrome. The word “palindromic” comes from the Greek Palin dromos, which means (backwards and around).
Palindromes are not always meaningful or sensible; they can be found in nature, art and literature. such as, “POP” is a palindrome string, as it is spelt the same back and forth. Similarly, “LEVEL” and “MADAM” are also palindromes because they read the same backwards as forward.
What is a Palindrome Number?
If you reversed a number and it seems the same as the original one, then the number is a palindrome number. Like, 121 remains the same when its digits are reversed (121 -> 121), hence it’s a palindrome. Similarly, 1001 and 11 are also palindrome numbers. As they remain the same on reversing their digits. Further, 1221 is a palindrome because it reads the same either from back or forth.
The numbers of palindrome numbers are infinite.
What is a Palindrome String?
Any word or sequence that is spelt or reads the same either from back or forth, then it is a palindrome string. Such as, “MALAYALAM” is a palindrome because it can be spelt either way around. Similarly, “NOON” and “CIVIC” are also palindrome strings because they read the same backwards as forward.
If a sequence of characters forms a palindrome when reversed in Python, then they are known to be a palindrome string.
What is a Palindrome Phrase?
A palindrome phrase is a phrase that is spelt the same as forth and back. For example, “Step on no pets” is a palindrome phrase as it can spell the same back or forth.
Some more examples are, “Do geese see God?” and “A man, a plan, a canal, Panama” are also palindrome phrases as they can also be spelt in the same way either from forth or back.
An algorithm for palindromes
If you want to check if a word, phrase, or number is a palindrome. we can use the following pseudocode to check for a palindrome:
- Firstly, reverse the word, phrase, or number.
- Then, check if reversed string or phrase is equal to the original or not.
- The word, phrase, or number will be a palindrome, if and only if the reversed version is the same as the original.
Programmes that use palindromes
Now that you have learned the basic structure of palindromes, let’s create some programs to check for a palindrome in python. However, we mostly used palindromes in:
- Decorating cakes and cookies with them (as in the case of my friend’s birthday party)
- Making people laugh by telling them your palindromes on purpose at parties and then getting everyone else laughing too!
Program 1: Palindrome string
The following codes check if a string is a palindrome. Then, prints a message indicating whether it is a palindrome or not. In this program, we have used the slicing method in the string.
def is_palindrome(s):
# Reverse the string
s_reversed = s[::-1]
# Compare the reversed string with the original
if s_reversed == s:
return True
else:
return False
# Test the function
print(is_palindrome("racecar"))
print(is_palindrome("hello"))
Code language: Python (python)
Output
True
False
Code language: Python (python)
Program 2: Palindrome string program
In this program, I have created two pointers. One left pointer is to check from the start of the string. Similarly, a right pointer checks from the end of the string(backwards). So it will optimise the program as it only runs half of the loop. So run time would be very less.
def isStringPalindrome(input_str):
#create two pointers
left = 0
right = len(input_str)-1
res = 1
#check if string is palindrome
for i in range(len(input_str)):
if input_str[left] != input_str[right] and (left < right):
res = 0
break;
left += 1
right -= 1
return res
# test the program
print(isStringPalindrome("hello"))
print(isStringPalindrome("MALAYALAM"))
print(isStringPalindrome("Codedamn"))
print(isStringPalindrome("madam"))
Code language: Python (python)
Output
0
1
0
1
Code language: Python (python)
Program 3: Palindrome number
This program utilizes a while loop to determine if the given number is a palindrome. To reverse the string, we have implemented a simple mathematical operation. In this program, you can see in p we have stored the remainder after division with 10. p will store the last digit of the number.
def isNumPalindrome(number):
# Reverse the string
x = number
reverse_num = 0
while(x):
p = x%10
reverse_num = (reverse_num)*10 + p
x = x//10
# Compare the two string
if reverse_num == number:
return 1
return 0
# Test the function
for i in range(3):
number = int(input("Enter a number:- "))
if isNumPalindrome(number):
print(f"Yes {number} is a palindrome.")
else:
print(f"No {number} is not a palindrome.")
Code language: Python (python)
Output
Enter a number:- 1221
Yes 1221 is a palindrome.
Enter the number:- 1789
No 1789 is not a palindrome.
Enter the number:- 144441
Yes 144441 is a palindrome.
Code language: Python (python)
This program can be very simple by using string properties in python.
def checkPalindrome(number):
# Convert the number to a string
number = str(number)
# Check if the number is the same forwards and backwards
return number == number[::-1]
# Test the function
print(checkPalindrome(121))
print(checkPalindrome(123))
Code language: Python (python)
Output
True
False
Code language: Python (python)
Program 4: Program using while loop
An example of how the program can be implemented in Python:
def checkPalindrome(string):
# Reverse the string
size = len(string)-1
reverse_string = ""
while(size>-1):
reverse_string = reverse_string + string[size]
size -= 1
# Compare the two strings
if reverse_string == string:
return True
return False
# Test the function
print(checkPalindrome("civic"))
print(checkPalindrome("level"))
Code language: Python (python)
Output
True
True
Code language: Python (python)
Program 5: Palindrome phrase
This program is to check if a given phrase is a palindrome or not. It uses some features of string in python.
def isPalindrome(string):
# Remove all non-alphanumeric characters and make the string lowercase
string = ''.join(c for c in string if c.isalnum()).lower()
# Check if the string is the same forwards and backwards
return string == string[::-1]
# Test the function
print(isPalindrome("A man, a plan, a canal: Panama"))
print(isPalindrome("race a car"))
Code language: Python (python)
Output
True
False
Code language: Python (python)
Conclusion
To summarize, a palindrome is a sequence of characters (such as a word, phrase, or number) that is spelt the same way forth and back. There are different possible ways to check the palindrome of a string or number in Python. We have shown you, five different programs that use different approaches, including using a join method, a while loop, and string slicing. All five programs demonstrate how to effectively check if a string or number is a palindrome in python. It is important to note that the programs may have different performance characteristics and may be more or less suitable for different use cases. However, regardless of the approach used, the basic concept of checking for palindromes remains the same.
Sharing is caring
Did you like what Srishti Kumari wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses:
303 students learning
Haris
Python Crash Course for Beginners
Surendra varma Pericherla
Learn Data Structures Using Python