Split Number Into Digits in Python
Here our objective is to split a given number into a series of separated digits. Let's suppose we have given the number 987654321 and we need to extract each of its digits in an integer value.
There are multiple ways to find split number into digits in python. Let's understand it in the following steps.
Scroll for More Useful Information and Relevant FAQs
- How to Separate digits from a given number using the map function?
- Program to print individual digits of a given Integer using for loop in python.
- How to extract digits from a number in Python using list comprehension?
- How to convert number into digits using a for loop & list?
- Feedback: Your input is valuable to us. Please provide feedback on this article.
How to Separate digits from a given number using the map function?
Map function executes a given function for each element of a given iterable such as tuple, list, etc.
Let understand with the following map function
mapObject = map(int, string_num)
- string_num is a list.
- Int is function to which map passed each element of given list string_num
- mapObject : Return Map object.
The following example illustrates map concepts to get desired Separate digits results.
giveNumber = 987654321 string_num = str(giveNumber) mapObject = map(int, string_num) separate_digit_list = list(mapObject) print(separate_digit_list)
Result: [9, 8, 7, 6, 5, 4, 3, 2, 1]
- First converted given number to str, str(giveNumber), for passing to map function as iterable. Required to convert str to support iteration.
- Map function executes int function for each of string_num elements.
- list(mapObject) Convert mapObject to the desired list of Separate numbers.
Program to print individual digits of a given Integer using for loop in python.
number = 987654321 for n in str(number): #print(type(n)) # Once enable will print <type 'str'> print(int(n)) # convert back to number from character
9 8 7 6 5 4 3 2 1
Explanation: We have given the number 987654321. For iterating numbers using a for loop, it requires converting into a string. If we display type on line 3, it will display
Now some of us may raise concerns about what if we iterate numbers rather than convert them into strings?
We can try to attempt to execute the following code and play around with type iterations.
number = 987654321 for n in number: print(int(n))
int object is not iterable
It doesnít allow to iterate numbers using a for loop in such a way and raise type error as int object is not iterable.
Therefore it requires converting from number to string before executing a loop as str(number). While iterating, each sequence represents a character and so it needs to convert each individual character to a number using int(n) to get the number sequence.
How to extract digits from a number in Python using list comprehension?
List comprehension gives powerful functionality in python to write code in a single line and achieve the desired result.
We have already gone through a code example demonstrating splitting numbers using a for a loop. Now we will rewrite the same program using List comprehension and understand techniques.
givenNumber = 987654321 charterList = [receivedDigit for receivedDigit in str(givenNumber)] print(charterList)
['9', '8', '7', '6', '5', '4', '3', '2', '1']
- Convert given number to string as str(givenNumber). Why? Explained here.
- List comprehension breaks the given string (converted from number) into discrete digits as defined as receivedDigit in the program.
- Collected list of digits (which is a list of characters) as charterList and print. /li>
Now we have received a list of individual characters rather than a list of individual numbers which was expected to be. Correct?
Just need to convert character to int within list comprehension. Here is the simple-to-read demonstration code
givenNumber = 987654321 charterList = [int(receivedDigit) for receivedDigit in str(givenNumber)] print(charterList)
Here int(receivedDigit) convert each character over iteration and final output become a list of individual numbers as expected as below
[9, 8, 7, 6, 5, 4, 3, 2, 1]
In such a way we can split a two-digit number, three-digit number, four-digit number, and so on.
How to convert number into digits using a for loop & list?
Following Code explains the list append function to collect each individual digit from a given number.
number = 987654321 result =  for n in str(number): result.append(int(n)) print(result)
Output: [9, 8, 7, 6, 5, 4, 3, 2, 1]
- Create an empty list (result = ) before the loop starts.
- Apply int function (int(n)) to convert each item from str to int value.
- Append integer value return from int(n) to result list.