473,230 Members | 1,594 Online

# example: 40286 -> 68204

Hi all,

Is there a fuction that reverse the digits of a number?

Many thanks,

Shihpin Lin

Jun 4 '07 #1
4 914
Shihpin said unto the world upon 06/03/2007 08:23 PM:
Hi all,

Is there a fuction that reverse the digits of a number?

Many thanks,

Shihpin Lin
This does it:
>>>def reversed_int(i):
... return int(''.join(reversed(str(i))))
...
>>>reversed_int(12345)
54321
>>>>
Best,

Brian vdB
Jun 4 '07 #2
Brian,

Thanks! I will try it when I come home,
Shihpin Lin

This does it:
>>def reversed_int(i):
... return int(''.join(reversed(str(i))))
...
>>reversed_int(12345)
54321

Best,

Brian vdB

Jun 4 '07 #3
On Jun 3, 5:23 pm, Shihpin <Shihpin....@gmail.comwrote:
Hi all,

Is there a fuction that reverse the digits of a number?

Many thanks,

Shihpin Lin

One can use int, str and a slice:

print int(str(40286)[::-1])

--
Hope this helps,
Steven

Jun 4 '07 #4
Thanks Steven, I will try to decipher that.

Shihpin
On 6 4 , 1:45, attn.steven....@gmail.com wrote:
On Jun 3, 5:23 pm, Shihpin <Shihpin....@gmail.comwrote:
Is there a fuction that reverse the digits of a number?
One can use int, str and a slice:

print int(str(40286)[::-1])

--
Hope this helps,
Steven

Jun 4 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.