**Palindrome Program in Python**

A word, phrase, number, or other sequence of units that can be read in both directions, in the same way, is called a palindrome, with general exceptions made for changes to word dividers and punctuation. Its reversed digits equal the original number when compared to the original. Numerical palindromes are also possible. For instance, 1234321, madam. We shall learn how to make a palindrome program in Python from this blog. Explore our Python course syllabus to gain expertise in fundamental coding.

**What is Palindrome?**

When a word, phrase, number, or other sequence of units is comma-separated, it can be read the same way in either direction and is called a palindrome. Check out how the goto statement in Python enhances code readability.

February 02, 2020, was a special day in 2020. Whether you prefer MM/DD/YYYY, DD/MM/YYYY, or YYYY/MM/DD as your chosen date format, it works. (02/02/2020 = 0202/02/02)

Suggested Read: Why should you learn Python?

**Types of Palindrome**

Three categories exist for palindromes: palindrome numbers, palindrome strings, and palindrome phrases, which are collections of words and special characters.

**Palindrome Number**

A group of numbers that don’t change when read backward is called a palindrome. It is also claimed that these numbers are symmetrical. Its reversed digits equal the original number when compared to the original. The instance of a palindrome is 9876789. Reversing its digits yields our initial number, 9876789, once more. There is no palindrome in 9876987. The new number, 7896789, is different from the original when it is reversed. Get a fundamental understanding of the Fibonacci series and try the codes with Python programming.

**Palindrome String**

An alphabetic collection that stays the same when read backward is called a palindrome string. Another name for them is Symmetric Alphabets. Its alphabet appears to be the same combination as the original string when put in reverse order. As an example, “noon” is a palindrome. It reverts to our initial string, “noon,” if its alphabets are switched. It is not a palindrome, “nine.” The new number, “enin,” is different from the original string when it is inverted. Learn about the importance of slicing substring in Python.

**Palindrome Phrase**

A palindrome phrase is a group of words and special characters that when read backward, retain their original meaning. It is also claimed that these words are symmetrical. The inverted version of the sentence is precisely the same as the original. Discover the key differences between C and Python.

Example: Step on no pets, Was it a cat I saw? etc.

**Palindrome Algorithm**

We should create a palindrome program in Python. Therefore, we need an algorithm.

Think about the Problem Statement’s algorithm: Determine whether a given string is a palindrome.

Step 1: Verify whether the first and last letters in the index match; if not, return false.

Step 2: Should be repeated, with the first index increased and the last index decreased.

Step 3: When first < last, repeat step 3. Return True if (first > last) is true.

Problem 1: Determine whether a given number is a palindrome

**Let’s now look at the problem statement algorithm: **

- To compare them later, duplicate the input number into a different variable.
- We then flip the supplied number. Use these steps to flip the number:

- Remove a number’s last digit. The remainder of a division is returned by the modulo operator (%)
- Move lastDigit to the back. (Reverse * 10) + lastDigit = reverse.
- Take the number’s last digit out. number is equal to number / 10.
- Repeat this procedure. while (number greater than zero)

- We now contrast the original number and the reversed number.
- A number is a palindrome if and only if the numbers add up to the same value.

Now that we have the algorithm, let’s apply similar reasoning to turn it into code. Enhance your skills by learning web development with Python at Softlogic Systems.

**Palindrome in Python**

Using the while loop for numbers

*number=int(input(“Enter any number :”))*

*temp=number*

*reverse_num=0*

*while(number>0):*

*digit=number%10*

*reverse_num=reverse_num*10+digit*

* ** **number=number//10*

*if(temp==reverse_num):*

* ** **print(“The number is a palindrome!”)*

*else:*

* ** **print(“Not a palindrome!”)*

**Output**

Enter any number: 12321

The number is a palindrome.

Also read: Amazing Guide on Python

Using the while loop for strings

*def check_palindrome(string):*

*length = len(string)*

*first = 0*

*last = length -1 *

*status = 1*

*while(first<last):*

*if(string[first]==string[last]):*

*first=first+1*

* ** **last=last-1*

* ** **else:*

* ** **status = 0*

* ** **break*

* ** **return int(status) *

*string = input(“Enter the string: “)*

*print(“Method 1”)*

*status= check_palindrome(string)*

*if(status):*

* ** **print(“It is a palindrome “)*

*else:*

* ** **print(“Sorry! Try again”)*

**Output**

Enter the string: malayalam

It is a palindrome

Suggested read: Format function in Python

Using ‘Reverse’ function

We may utilize the reverse function in Python for checking palindromes. If a word is a palindrome, then we know that it can be read both forward and backward. As a result, let’s create the forward and backward strings for the same and see whether they match.

*def check_palindrome_1(string):*

*reversed_string = string[::-1]*

*status=1*

*if(string!=reversed_string):*

*status=0*

* ** **return status*

*string = input(“Enter the string: “)*

*status= check_palindrome_1(string)*

*if(status):*

* ** **print(“It is a palindrome “)*

*else:*

* ** **print(“Sorry! Try again”)*

**Output**

Enter the string: MOM

It is a palindrome

Palindrome Program in Python

Here are several approaches to implementing the Python palindrome program. Data analysis with Python will be easy if you learn the concept of the palindrome program.

Palindrome String

**Method 1:**

- Calculating a string’s reverse
- Verifying whether or not the original and reverse are the same

*def isPalindrome(s):*

*return s == s[::-1]*

*s = “kayak”*

*ans = isPalindrome(s)*

*if ans:*

*print(“Yes”)*

*else:*

*print(“No”)*

Steps:

- We develop an ispalindrome function.
- Slice the parameter in reverse to return a variable.
- The string that we wrote in our driver code
- Lastly, we execute whether it is a palindrome, print yes, or print no in our if-else condition.

**Method 2:**

Using an iterative loop

def isPalindrome(str):

for i in range(O, int(len(str)/2)):

if str[i] != str[len(str)-i-1]:

return False

return True

s = “deed”

ans = isPalindrome(s)

if (ans):

print(“Yes”)

else:

print(“No”)

**Steps:**

- A loop is executed from the beginning to half of the string’s length, examining each character from the first to the end.
- Additionally, verify the string from the second to the second-to-last character.
- It is not a palindrome if there are any mismatched characters.

Gain expertise with data structures and algorithms in Python through our recent article.

**Method 3:**

Using the built-in function

def isPalindrome(s):

rev = ‘’.join(reversed(s))

if (s == rev):

return True

return False

s = “civic”

ans = isPalindrome(s)

if(ans):

print(“Yes”)

else:

print(“No”)

We are using the predefined function “.join” in this approach.

Explore how multithreading in Python is accomplished through our blog.

Method 4: Using Recursion

def isPalindrome(s):

s = s.lower()

1 = len(s)

if 1 <2:

return True

elif s(0) == s{l – 1):

return isPalindrome(s[1: l – 1])

else:

return False

s = “radar”

ans = isPalindrome(s)

if ans:

print(“Yes”)

y else:

print(“No”)

This technique calls the remainder of the substring recursively after comparing the string’s initial and last elements.

Suggested article: Python developer salary in India

Enroll in our Python training in Chennai to get thorough hands-on exposure to real-time projects.