Split Integer into Digits in Python

Using List Comprehension

To split integer into digits in Python:

  • Use the str() to transform the specified integer to a string.
  • Use a list comprehension to loop over converted String.
  • Use int() to convert every substring to an integer in each iteration.

This code will display the following results.

While using list comprehension in the above code block, we converted the given number to a string using the str() method. Then, we used list comprehension to iterate over the converted string..

We used int() in each iteration to convert the sub-string to an integer. All this code (int(i) for i in str(number)) is enclosed in [] to get list-type results.

Using for Loop

To split number into digits in Python:

  • Use the str() to transform the specified integer to a string.
  • Use the for loop to iterate over the converted string.
  • Use int() to convert every substring to an integer in each iteration. Then, pass the converted integer to the append() method to append elements to a list.

First, we used str() and passed the specified number to this function to convert from integer to string. Then, we used a for loop to iterate over the converted string.

In each iteration, we first converted the sub-string to an integer and passed it to the append() method, which is used to append items to a list named list_of_digits.

Using map() with list() & str.split()

To split the given number into digits in Python:

  • Use str() to convert the specified number to string to make it iterable.
  • Use map() to run a particular function for each substring in the iterable (we created in the previous step).
  • Use list() to create a list object.

After executing the above code successfully, we will get the following output on the console.

For this code example, we used the str() method, which took the number as a parameter and converted it to a string. Why did we need to convert it? Because we want to make it iterable to use the map() function.

The map() took two parameters, the first parameter is the int function that we wanted to execute, and the second is the iterable returned by str(number). Here, the map() function converted each element (which is sub-string here) of an iterable to an integer.

After that, we used the list() function to create a list object. Here, the list object is a collection that is ordered and changeable.

Suppose we are given a number in string format already; how can we convert them to digits? In that case, we can do as follows:

  • Use str.split() to split the given string.
  • Use the map() function to iterate over each list element and convert it to an integer.
  • Use the list() function to convert it to a list object.

Now, we will get the output as follows.

The map() are list() discussed while explaining the code using map() with list(). In this code snippet, we used a new function, str.split(), it is used to split the specified string based on the provided separator.

The separator can be whitespace, a comma, a dash or anything. Here, we are splitting our string using its default separator, which is a whitespace. You can find more on that here.

That’s all about how to split Integer into Digits in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *