Reverse Iteration Through Integers

Lie Ryan lie.1296 at gmail.com
Sun Oct 18 17:45:16 EDT 2009


Benjamin Middaugh wrote:
> 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,
> 
> Benjamin "Self-Proclaimed Python Newbie"

you need to turn the int to a str:
 >>> n = 169
 >>> rn = int(''.join(reversed(str(n))))
 >>> rn
961
 >>>



More information about the Python-list mailing list