Reverse Iteration Through Integers

Christian Heimes lists at cheimes.de
Sun Oct 18 17:51:20 EDT 2009


Benjamin Middaugh schrieb:
> I'm trying to make an integer that is the reverse of an existing integer 
> such that 169 becomes 961. I guess I don't know enough yet to figure out 
> how to do this without a ton of awkward-looking code. I've tried for 
> loops without much success. I guess I need a good way of figuring out 
> the length of the input integer so my loop can iterate that many times 
> to reverse the number, but I keep getting errors that say "TypeError: 
> 'Int' object is not iterable".
> 
> Any help would be much appreciated,

Here is a simplistic version that doesn't use fancy math:

>>> str(24)
'24'
>>> str(24)[::-1]
'42'
>>> int(str(24)[::-1])
42




More information about the Python-list mailing list