How can I write code to find a palindrome in python without using string functions

0 votes

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")

Thanks in advance..

Jan 16, 2019 in Python by aryya
• 7,460 points
22,990 views

2 answers to this question.

0 votes

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[0]

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!

answered Jan 16, 2019 by charlie_brown
• 7,720 points
0 votes
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

Related Questions In Python

0 votes
2 answers

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

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

answered Oct 25, 2020 in Python by anonymous
23,868 views
+1 vote
1 answer
0 votes
0 answers

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

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

Sep 21, 2022 in Python by Samuel
• 460 points
578 views
0 votes
0 answers

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

Nov 24, 2022 in Python by sarit
• 1,830 points
584 views
+5 votes
6 answers

Lowercase in Python

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

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,108 views
0 votes
1 answer

Removing surrounding whitespace

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

answered May 4, 2018 in Python by Nietzsche's daemon
• 4,260 points
784 views
0 votes
1 answer

How to correctly return an a dictionary as an output in zappier code using python?

David here, from the Zapier Platform team. ...READ MORE

answered Dec 3, 2018 in Python by charlie_brown
• 7,720 points
1,674 views
+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 4, 2018 in Python by charlie_brown
• 7,720 points
13,571 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP