+ 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.....

26 odpowiedzi

+ 10

s = input("Enter a number")
print(f"{s} is{" " if s == s[::-1] else " not "}a palindrome.")

+ 9

c=c//10
makes the difference 😉
https://www.geeksforgeeks.org/division-operator-in-JUMP_LINK__&&__python__&&__JUMP_LINK/

+ 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.

+ 4

RAJESH SAHU Tomiwa Joseph
s=str(int(input())
is the savior 😉

+ 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

+ 3

Tomiwa Joseph Nikhil Anand
is 0131 a pallindrome number?

+ 2

Can you explain what the code is supposed to do. Give some test cases if you can

+ 2

Justus palindrome of a number

+ 2

Tomiwa Joseph oo I'll keep that in mind thank you......but I couldn't get the hint...... maybe bcuz I just started

+ 2

Oma Falk yes

+ 2

Tomiwa Joseph that makes it a bit more complicated with your(absolut good) idea

+ 2

Oma Falk Tomiwa Joseph help me why....it isn't absolutely a palindrome

+ 2

But I think left most is least significant value and it's 0 so 0131=131

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

+ 2

Brilliant!!! This is another way to look it Oma Falk Best way I think.

+ 2

Oma Falk Yes It is🤗

+ 1

Interesting question. Not sure. Is there a natural number like that?

+ 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.

+ 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

+ 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