palindrome in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

palindrome in python

"""Program to check a numbr is palindrome or not""" n=int(input("Enter a Number\n")) c=n rev=0 print("Entered integer is "+str(int(c))) while c>0: rem=c%10 rev=(rev*10)+rem c=c/10 if(rev==n): print(str(int(n))+" is palindrome") else: print(str(int(n))+" is not palindrome") although i am a beginner but why does this above code won't give correct answer on python3 whereas it on python 2.7 it gives correct one..... please help.....

22nd Apr 2020, 4:26 PM
Nikhil Anand
Nikhil Anand - avatar
26 Answers
+ 10
s = input("Enter a number") print(f"{s} is{" " if s == s[::-1] else " not "}a palindrome.")
23rd Apr 2020, 5:10 AM
David Ashton
David Ashton - avatar
22nd Apr 2020, 4:54 PM
Oma Falk
Oma Falk - avatar
+ 4
First of, you didn't have to convert the user input to an integer only for you to convert it back to string. The hint I'll give you is the use of [::-1] in your conditional statement. Find out what [::-1] means.
22nd Apr 2020, 4:55 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 4
RAJESH SAHU Tomiwa Joseph s=str(int(input()) is the savior 😉
22nd Apr 2020, 5:57 PM
Oma Falk
Oma Falk - avatar
+ 3
Ok, I'll give you the last hint. It is up to you how you use it in your code😀😀😀 [::-1] means like reverse. For example: a = boy b = a[::-1] print (b) Output: yob
22nd Apr 2020, 5:15 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 3
Tomiwa Joseph Nikhil Anand is 0131 a pallindrome number?
22nd Apr 2020, 5:18 PM
Oma Falk
Oma Falk - avatar
+ 2
Can you explain what the code is supposed to do. Give some test cases if you can
22nd Apr 2020, 4:38 PM
Justus
Justus - avatar
+ 2
Justus palindrome of a number
22nd Apr 2020, 5:02 PM
Nikhil Anand
Nikhil Anand - avatar
+ 2
Tomiwa Joseph oo I'll keep that in mind thank you......but I couldn't get the hint...... maybe bcuz I just started
22nd Apr 2020, 5:03 PM
Nikhil Anand
Nikhil Anand - avatar
+ 2
22nd Apr 2020, 5:20 PM
Nikhil Anand
Nikhil Anand - avatar
+ 2
Tomiwa Joseph that makes it a bit more complicated with your(absolut good) idea
22nd Apr 2020, 5:23 PM
Oma Falk
Oma Falk - avatar
+ 2
Oma Falk Tomiwa Joseph help me why....it isn't absolutely a palindrome
22nd Apr 2020, 5:25 PM
Nikhil Anand
Nikhil Anand - avatar
+ 2
But I think left most is least significant value and it's 0 so 0131=131
22nd Apr 2020, 5:30 PM
Nikhil Anand
Nikhil Anand - avatar
+ 2
s=input("enter string") r='' tmp=s l=len(s) while(l>0): r=r+s[l-1] l=l-1 if tmp==r: print ("palindrome") else: print ("not")
22nd Apr 2020, 5:43 PM
ANJALI SAHU
+ 2
Brilliant!!! This is another way to look it Oma Falk Best way I think.
22nd Apr 2020, 6:00 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 2
Oma Falk Yes It is🤗
22nd Apr 2020, 6:12 PM
ANJALI SAHU
+ 1
Interesting question. Not sure. Is there a natural number like that?
22nd Apr 2020, 5:22 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 1
Think about it this way. If he wants to find out if the input is a palindrome or not, then 0131 is not a palindrome.
22nd Apr 2020, 5:26 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 1
you make covert to string and make check i and len-1-i char index of the string. Something like this k = len(s) for i in range(k//2): if s[i]!=s[k-1-i]: # not palindrome break else: # palindrome i wrote this code on c++ several years ago but i think you can change it to py https://code.sololearn.com/cJP8W94DrTat/?ref=app
23rd Apr 2020, 8:38 PM
george
george - avatar
+ 1
s = input() # check if s is number via spec function to make 0123 as 123 and so on. #then s1 = str(s[::-1]) s1== s it is palindrome. Since we know that s is string contains digits, s1 reversed also contain digits and s1 is equal to s so it is palindrome.but time of this function os mpre than i write above
23rd Apr 2020, 8:46 PM
george
george - avatar