d=["a","b","c","d","e"] w="abcde" p=2 ch="" for w1 in w: sx=d.index(w1) sx=sx+p if sx>4:sx=sx-5 ch=ch+d[sx] print(ch) px=-p rev="" for w1 in ch: sx=d.index(w1) sx=sx+px if sx>4:sx=sx-5 if sx<0:sx=sx+5 rev=rev+d[sx] print(rev)