Python Program to Split Number into Digits
You can split a number into digits in Python by converting it to a string and using a list comprehension like
[int(d) for d in str(number)] to get each digit as an integer.Examples
Input123
Output[1, 2, 3]
Input4050
Output[4, 0, 5, 0]
Input7
Output[7]
How to Think About It
To split a number into digits, think of the number as a sequence of characters. Convert the number to a string so you can look at each character one by one. Then, change each character back to a number to get the digits.
Algorithm
1
Get the input number.2
Convert the number to a string to access each digit.3
For each character in the string, convert it back to an integer.4
Collect all these integers into a list.5
Return or print the list of digits.Code
python
number = 12345 digits = [int(d) for d in str(number)] print(digits)
Output
[1, 2, 3, 4, 5]
Dry Run
Let's trace the number 12345 through the code
1
Convert number to string
str(12345) -> '12345'
2
Iterate over each character
Characters: '1', '2', '3', '4', '5'
3
Convert each character to int
int('1')=1, int('2')=2, int('3')=3, int('4')=4, int('5')=5
4
Collect digits into list
[1, 2, 3, 4, 5]
| Character | Digit |
|---|---|
| '1' | 1 |
| '2' | 2 |
| '3' | 3 |
| '4' | 4 |
| '5' | 5 |
Why This Works
Step 1: Convert number to string
Using str(number) turns the number into a sequence of characters, making it easy to access each digit.
Step 2: Iterate over characters
Looping through the string lets us handle one digit at a time.
Step 3: Convert characters back to integers
Each character is converted back to an integer with int() to get the actual digit value.
Alternative Approaches
Using math operations
python
number = 12345 digits = [] while number > 0: digits.append(number % 10) number //= 10 digits.reverse() print(digits)
This method uses division and modulus to extract digits from the end, then reverses the list to get the correct order.
Using map and str
python
number = 12345 digits = list(map(int, str(number))) print(digits)
This uses <code>map()</code> to apply <code>int</code> to each character, which is clean and readable.
Complexity: O(n) time, O(n) space
Time Complexity
The program loops through each digit once, so time grows linearly with the number of digits.
Space Complexity
A new list stores all digits, so space also grows linearly with the number of digits.
Which Approach is Fastest?
Both string conversion with list comprehension and math operations have similar time complexity, but string methods are simpler and more readable.
| Approach | Time | Space | Best For |
|---|---|---|---|
| String conversion with list comprehension | O(n) | O(n) | Simplicity and readability |
| Math operations with modulus and division | O(n) | O(n) | Avoiding string conversion |
| Using map with str | O(n) | O(n) | Clean functional style |
Convert the number to a string first to easily access each digit.
Trying to split the number directly without converting it to a string causes errors because numbers don't support iteration.