473,549 Members | 2,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

removing a digit from a integer

1 New Member
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
1 11452
bvdet
2,851 Recognized Expert Moderator Specialist
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

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

Similar topics

5
26630
by: Bunyip Bluegum | last post by:
I have a text field in a form which I need to check to see that only a 4-digit integer has been entered. The field has MAXLENGTH=4 and I'm using this to check for length: function checkpostcode(form) { var min=4; if (form.postcode.value.length < min) { alert("Please enter a 4-digit postcode"); return false; }
38
6892
by: Alan | last post by:
I want to change a 3 digits integer to characters, how can i do that? the 3 digits integer maybe 123, 23 or 3 I want to change the integer to "123", " 23" or " 3" thx
8
6548
by: Candace | last post by:
I am using the following code to pick off each digit of a number, from right to left. The number I am working with is 84357. So for the first iteration it should return the number 7 and for the second iteration it should return the number 5, and so on. But for some reason on the first iteration returns the expected results. Each subsequent...
3
2215
by: PeyYang | last post by:
Hi This spring I'm taking a Java coures and I need some help if posseble. I have this assignment and he needs me to use the modulus in the code: Q:Your application should read a four-digit integer entered by the user in an input dialog and encrypt it as follows: Replace each digit by (the sum of that digit plus 7) modulus 10. The swap...
7
2199
by: southernsunsations | last post by:
/******************************************************************\ * * * * Date: 09.06.2007 * \******************************************************************/ // Example: num digit // A // B...
14
8643
by: thehobbit | last post by:
Hi, Could anyone give ideas on how to add 4 20 digit numbers in ANSI C and pass the result back to a calling program in COBOL? We were able to add up to 15 digit numbers without any problems, but we started facing issues once we go above 15 digits. Thanks, Venkat
5
3053
by: toralf | last post by:
Hello, I'm wondering how I can test whether a field matches a 10-digit integer within DB2. -- MfG/Sincerely Toralf Förster pgp finger print: 7B1A 07F4 EC82 0F90 D4C2 8936 872A E508 7DB6 9DA3
5
1596
by: =?Utf-8?B?Y2hyaXNiZW4=?= | last post by:
Hi, I try to use an integer to pass 4 kinds of information. Each kind may have 5 types. I plan to use an integer like 2034, while 2 0 3 4 are corresponding to the type of each kind. I am wondering, what is the most efficient way to 1. update any of field, for example, changing from 2034 to 2134 2. grab the number (0 to 4) from any one...
0
7518
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7446
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7715
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7808
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5087
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.