In Python, the integer is represented as any value other than decimals. The individual digits of an integer are not accessed directly because the integer is not iterable in Python. Different methods are used in Python to split the integer value into digits.
This Python guide will demonstrate several methods to split integers into digits using various examples. The content supported by this guide is provided below:
- Method 1: Use str() and int() Functions in List Comprehension
- Method 2: Use str() Function With map()
- Method 3: Use Simple for Loop
- Method 4: Use math.ceil() With math.log() Function
- Method 5: Use divmod() Function
Let’s start with the first method.
Method 1: Use str() and int() Function in List Comprehension
Python’s inbuilt “str()” function typecast the input value into a string and “int()” typecast it into an integer. In the example given below, the “str()” and “int()” are used inside the List Comprehension to split an integer into digits:
Code:
integer = 1947
output = [int(x) for x in str(integer)]
print(output)
print(type(output))
In the above code:
- The int is converted into strings and the “for” loop iterates over the index of strings.
- On each iteration, the string is converted back into int and represented as an element of the list.
- The List comprehension splits every digit of an integer into a list.
Output:
The integer “1947” is split into separate digits and represented as a list of elements.
Method 2: Use str() Function With map()
The “map()” function is used in Python to transform all the elements in a sequence without using an explicit for loop. The “str()” function is used along with the “map()” function to split an int value into digits in Python:
Code:
integer = 56786
string = str(integer)
output = map(int, string)
print(list(output))
In the above code:
- The integer value is initialized and converted into strings using the “str()” function.
- The “map()” function accepts two parameters, the string variable and the integer, and returns the integer map object.
- The “list()” function converts the map iterator object into a list.
Output:
The integer value is split into digits using the map() function.
Method 3: Use Simple for Loop
The “for” loop is used to iterate over the strings and return each index value by appending it into an empty list using the append() function. The example shown below will provide the working of this approach:
Code:
integer = 56786
string = str(integer)
empty = []
for items in string:
empty.append(int(items))
print(empty)
In the above code:
- The integer value is converted into strings utilizing the “str()” function.
- The for loop iterates over the strings and appends each item of the string into a list using the “append()” function.
- The final empty list shows that the integer is split into digits.
Output:
The above output verified that the integer value had been spilled into digits using the “for” loop and “append()” functions.
Method 4: Use math.ceil() With math.log() Function
The math module function “math.ceil()” and “math.log()” is used along with the combination in List Comprehension to split an int value into digits. Here is an example:
Code:
import math
integer = 56786
y = math.log(integer, 10)
z = math.ceil(y)
x = [(integer//(10**i))%10 for i in range(z-1, -1, -1)]
print(x)
In the above code:
- The math.log() function returns a natural algorithm by accepting the input integer as a first parameter and base “10” as a second parameter.
- The “math.ceil()” function accepts the variable “y” as an argument and returns the number up to the nearest integers.
- The list comprehension approach is used to iterate over the range and split each integer value into separate digits.
Output:
The above output shows that the input integer has been split into digits.
Method 5: Use divmod() Function
In Python, the “divmod()” function is used to return the tuple value that contains the remainder and quotient. The below code shows how we use the “divmod()” to split an integer into digits in Python:
Code:
int_num = 8765
empty = []
while int_num > 0:
int_num, remd = divmod(int_num, 10)
empty.append(remd)
print(empty)
In the above code, the “divmod()” function accepts the integer value as an argument. It splits an integer into digits by constantly dividing the integer value by “10” and taking the remainder. The remainder value is then appended into an empty list, representing each value as separate digits.
Note: The “divmod() ”function retrieves the digits in reverse order, so to get the list in the original order, we need to use the “reversed()” function.
Output:
The above output shows that the input integer has been split into digits.
These are all the methods to split an integer into digits in Python.
Conclusion
To split an integer into digits, the “str()” and “int()” in List Comprehension, “str()” with “map()”, for Loop, “math.ceil()” with “math.log()” function, and “divmod()” function are used in Python. The “str()” function converts the integer into strings and, for loop, iterates over the strings to split each index value into separate digits. The simple for loop is also used with the “append()” function to iterate over the strings and append the value into an empty list. This article detailed a guide on how to split an integer into digits in Python.