471,325 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

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 871
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Michael Foord | last post: by
6 posts views Thread by Ville Vainio | last post: by
5 posts views Thread by relaxedrob | last post: by
2 posts views Thread by Xah Lee | last post: by
25 posts views Thread by meltedown | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.