# How to check whether a string is odd palindrome in python?

• shivajikobardan
In summary: The key to all this is that if n is an even, positive integer, then n // 2 and (n + 1) // 2 both have the same value.What happens if you use string lengths of 0 or 1 ? If you use string lengths of 0 or 1, the loop runs the same number of times, but the value of isPalindrome is different. When n is 0, isPalindrome is False, and the loop exits. When n is 1, isPalindrome is True, and the loop prints the value of isPalindrome.

#### shivajikobardan

I am learning to code and 1 thing that surprises me is how do I internalize all the code? I understand the code. I know the algorithm as well. But I want to be able to solve any types of problems(related ones) after learning 1 code. How do I become able to do that? So for that I am first trying with palindrome program.
Here is the palindrome program for even palindrome.

Code:
#palindrome checking

str1="abba"
for i in range(len(str1)//2):
if(str1[i]==str1[len(str1)-i-1]):
isPalindrome=True
else:
isPalindrome=False
print(isPalindrome)
Now I want to write code for odd palindrome. Don't show me code but show me direction or algorithm so that I can write code on my own.
example of odd palindrome is abbcbba. We are using c as the middle point.

It's the same method except that you first remove (or ignore) the middle letter to transform it into an even palindrome.

abbXbba is equivalent of checking abbbba, no matter what X is.

shivajikobardan
jack action said:
It's the same method except that you first remove (or ignore) the middle letter to transform it into an even palindrome.
Exactly the same code as posted works with either odd-length or even-length palindromes. You don't need to remove the middle letter.

For example, if the string is "abba", the loop runs for i = 0 and i = 1, because range(len(str1)//2) includes 0 and 1. If the string is "abcba", the loop also runs for i = 0 and i = 1, because range(len(str1)//2) still includes only 0 and 1.

In case the above isn't clear, for the shorter string, len(str1) == 4, so len(str1)//2 == 2. The call to range(2) is the sequence 0, 1. For the longer string, len(str1) == 5, so len(str1)//2 == 2 again. The call to range(2) is still the sequence 0, 1.

So for the shorter string, the loop runs 2 times; i.e., for i = 0 and for i = 1. When i = 0, the loop compares str1[0] and str1[3] (both are 'a'), and sets isPalindrome to True. When i = 1, the loop compares str1[1] and str1[2] (both are 'b'), and resets isPalindrome to True. The loop then exits, and prints the value of isPalindrome.

For the longer string "abcba", the loop also runs 2 times. When i = 0, the loop compares str1[0] and str1[4] (both are 'a'), and sets isPalindrome to True. When i = 1, the loop compares str1[1] and str1[3] (both are 'b'), and resets isPalindrome to True. The loop then exits, and prints the value of isPalindrome. Note that the loop, as written, doesn't even look at the middle character.

The key to all this is that if n is an even, positive integer, then n // 2 and (n + 1) // 2 both have the same value.

Last edited:
shivajikobardan and jack action
What happens if you use string lengths of 0 or 1 ? and is that the result your want ?

## What is a palindrome?

A palindrome is a word, phrase, or sequence that reads the same backward as forward, such as "radar" or "madam".

## How can I check if a string is a palindrome in python?

One way to check if a string is a palindrome in python is to reverse the string and compare it to the original. If they are the same, then the string is a palindrome.

## How do I reverse a string in python?

To reverse a string in python, you can use the built-in function "reversed()" or use slicing with a negative step (-1).

## Can a string be an odd palindrome?

Yes, a string can be an odd palindrome. This means that the string has an odd number of characters and the middle character remains unchanged when the string is reversed.

## How do I check if a string is an odd palindrome in python?

To check if a string is an odd palindrome in python, you can use the same method as checking for a palindrome, but also check if the length of the string is odd.