PencilCrate PencilCrate - 7 months ago 10
Python Question

Checking for a number if first 4 digits forward equals last 4 digits backwards

If the last 4 elements stepping forward are equal to the last 4 elements stepping backwards, print the number. I have input a number where the last 4 elements are clearly palindromic. Why does this not print the number?

def checkNum(i):
num = str(i)
if num[len(num)-5:len(num)-1:1] == num[len(num)-5:len(num)-1:-1]:
print(num)
checkNum(777777)

Answer
>>> num = str(777777)
>>> print num[len(num)-5:len(num)-1:1]
7777
>>> print num[len(num)-5:len(num)-1:-1]
***None***

To access the last four from the last position, you need

>>> print num[len(num)-1:len(num)-5:-1]
7777

def checkNum(i):
    num = str(i)
    if num[len(num)-5:len(num)-1:1] == num[len(num)-1:len(num)-5:-1]:
        print(num)

>>> checkNum(777777)
777777