By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,661 Members | 781 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,661 IT Pros & Developers. It's quick & easy.

removing a digit from a integer

P: 1
i want to know how to remove a digit from an integer and place that digit at the beginning of the number. I know how to do it in a list but am not sure how to do in without creating a list.
eg.
digits(123)
>>>312
Feb 27 '14 #1
Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
This uses a str object, but not a list:
Expand|Select|Wrap|Line Numbers
  1. >>> i = 123
  2. >>> s = str(i)
  3. >>> int(s[-1]+s[:-1])
  4. 312
  5. >>> 
This doesn't use any type of sequence:
Expand|Select|Wrap|Line Numbers
  1. i = 123456789
  2. ii = i
  3. j = 0
  4. while ii:
  5.     ii, k = divmod(ii, 10)
  6.     j += 1
  7. n, x = divmod(i, 10)
  8. print x * 10**(j-1) + n
Feb 27 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.