ID : 74

viewed : 115

Tags : PythonPython IntegerPython String

93

This tutorial will discuss different methods to split an integer into digits in Python.

List comprehension is a much shorter and graceful way to create lists that are to be formed based on given values of an already existing list.

In this method, `str()`

and `int()`

functions are also used along with List comprehension to split the integer into digits. `str()`

and `int()`

functions are used to convert a number to a string and then to an integer respectively.

The following code uses list comprehension to split an integer into digits in Python.

`num = 13579 x = [int(a) for a in str(num)] print(x) `

Output:

`[1, 3, 5, 7, 9] `

The number `num`

is first converted into a string using `str()`

in the above code. Then, list comprehension is used, which breaks the string into discrete digits. Finally, the digits are converted back to an integer using the `int()`

function.

`math.ceil()`

and `math.log()`

Functions to Split an Integer Into Digits in PythonThe operation of splitting the integer into digits in Python can be performed without converting the number to string first. Moreover, this method is about twice as fast as converting it to a string first.

The `math.ceil()`

function rounds off a number up to an integer. The `math.log()`

function provides the natural logarithm of a number. To use both these functions, we should import the `math`

library.

The `math`

module can be defined as an always accessible and standard module in Python. It provides access to the fundamental C library functions.

The following code uses list comprehension, `math.ceil()`

and `math.log()`

functions to split an integer into digits in Python.

`import math n = 13579 x = [(n//(10**i))%10 for i in range(math.ceil(math.log(n, 10))-1, -1, -1)] print(x) `

Output:

`[1, 3, 5, 7, 9] `

`map()`

and `str.split()`

Functions to Split an Integer Into Digits in PythonThe `map()`

function implements a stated function for every item in an iterable. The item is then consigned as a parameter to the function.

The `split()`

method, as the name suggests, is used to split a string into a list. It has a basic syntax and holds two parameters, `separator`

, and the `maxsplit`

.

The number needs to be already in the string format so that this method could be used.

The following code uses the `map()`

and `str.split()`

functions to split an integer into digits in Python.

`str1 = "1 3 5 7 9" list1 = str1.split() map_object = map(int, list1) listofint = list(map_object) print(listofint) `

Output:

`[1, 3, 5, 7, 9] `

Here, we used the `str.split()`

method to split the given number in string format into a list of strings containing every number. Then the `map()`

function is used, which is utilized to generate a map object which converts each string into an integer. Finally, `list(mapping)`

is used to create a list from the map object.

`for`

Loop to Split an Integer Into Digits in PythonIn this method, we use a loop and perform the slicing technique till the specified number of digits (`A=1`

in this case) and then finally, use the `int()`

function for conversion into an integer.

The following code uses the `int()`

+loop+slice to split an integer into digits in Python.

`str1 = '13579' # initializing substring A = 1 # create a result list result = [] for i in range(0, len(str1), A): # convert to int, after the slicing process result.append(int(str1[i : i + A])) print("The resultant list : " + str(result)) `

Output:

`The resultant list : [1, 3, 5, 7, 9] `