I'm a beginner in Python. I've written palindrome programs without using string functions.

Question 1: Why does the 1st logic not give me the desired output? Correct me if I'm wrong.

Question 2: In the 2nd program if I skip i += 1 in if statement it should give me the correct result as the for loop automatically increments value of i. Rather it gives me correct output only if I include that statement or else not. What may be the reason for this?

Logic 1:

```n = input("eNTER  STRING\n")
length = int(len(n))
n = str(n)

for i in range(0, int(length/2+1)):
if n[i] != n[-i - 1]:
break

if i < int(length/2 + 1):
print("not")
else:
print("yes")```

Logic 2:

```n = input("ENTER  STRING\n")
length = int(len(n))

for i in range(0, int(length/2 + 1)):
if n[i] == n[-i - 1]:
i += 1
else:
break

if i < (length / 2):
print("not")
else:
print("yes")```

Jan 16, 2019 in Python 17,985 views

## 2 answers to this question.

In Logic 1, try if i<int(length/2): instead of if i<int((length/2+1)):

In Logic 2, even removing i+=1 gives the correct result:

```if n[i] == n[-i-1]:
pass
else:
break```

You can also use the following code for the same:

```def reverse(text):
if len(text) <= 1:
return text
return reverse(text[1:]) + text

n = input("ENTER  STRING\n")
if n==reverse(n):
print ("It's a palindrome")
else:
print ("It's not a palindrome")```

Hope this Helps!!

To learn more, join the online course to do Masters in Python.

Thanks!

• 7,720 points
```x="malayalam"
y=""
for i in x:
y=i+y
if y==x:
print("yes",y,"is palindrome")
else:
print("not",y,"is not a palindrome")

```
answered Nov 3, 2020 by Thangaraj

## How can I write a program to add two numbers using functions in python?

there is sum() function as a built ...READ MORE

+1 vote

## I am trying to write a program in python for the occurrences of a character given the start and stop values of that string only using while

Here's the code: check = input("Enter the character: ...READ MORE

## How can I convert a list to a string using Python?

How can I convert a list to ...READ MORE

## How can I find the first occurrence of a sub-string in a python string?

I'd like to locate the first index ...READ MORE

## how do i change string to a list?

suppose you have a string with a ...READ MORE

## Lowercase in Python

You can simply the built-in function in ...READ MORE

## Removing surrounding whitespace

Try the strip() function:  s = s.strip() If you ...READ MORE

## How can I arrange the words in ascending order?

Yes there is a function in python ...READ MORE