-1

Below is my code:

def check_palindrome(num):
    my_str = str(num) 
    my_list1 = list(my_str) 
    my_list2 = list(my_str)
    my_list2.reverse()
    if my_list1 == my_list2:
        return "It's palindrome"
    else:
        return "Not a palindrome"
    
print(check_palindrome(232))
print(check_palindrome(235))

If I execute the code in a terminal it works, but I'm not sure if is it short and nead code or not?

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

1

You can do it like this:

def check_palindrome(num) -> bool:
    my_str = str(num)
    if my_str == my_str[::-1]:
        return True
    return False

EDIT even shorter (and neater): credits @mozway

def check_palindrome(num) -> bool:
    my_str = str(num)
    return my_str == my_str[::-1]:

I did not return a string but you can do that as wel:


if check_palindrome(num):
    print('It is a palindrome')
else:
    print('It is not a palindrome')
3dSpatialUser
  • 2,034
  • 1
  • 9
  • 18