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?


import_contacts

You may also like:

Related Posts

  • 27 December

    Create Array of All NaN Values in Python

    Table of ContentsUsing numpy.empty() FunctionUsing numpy.full() FunctionUsing numpy.tile() FunctionUsing numpy.repeat() FunctionUsing Multiplication of numpy.ones() with nan Using numpy.empty() Function To create an array of all NaN values in Python: Use numpy.empty() to get an array of the given shape. Assign numpy.nan to every array element using the assignment operator (=). [crayon-63dabcd9d0a0e445559857/] [crayon-63dabcd9d0a18514472378/] We used numpy.empty() […]

  • 27 December

    Call Function from Another Function in Python

    Table of ContentsCall a Function in PythonCall Function from Another Function in PythonCall a Function from Another Function within the Same/Different Classes Call a Function in Python To call a function in Python: Write a test() function, which prints a message. Call the function defined in the previous step. [crayon-63dabcd9d0c43048639819/] [crayon-63dabcd9d0c48845274223/] To call a function, […]

  • 27 December

    [Fixed] NameError Name ‘unicode’ is Not Defined in Python

    Table of ContentsUse str() MethodUse sys.version_info with strUse six.text_type Use str() Method To resolve the NameError: name 'unicode' is not defined, replace the occurrence of unicode() with str(). This issue occurs because unicode() was renamed to str() in Python 3. [crayon-63dabcd9d0d7e311339668/] [crayon-63dabcd9d0d81068939233/] Unicode is a computing industry standard that ensures that text from most of […]

  • 26 December

    Repeat List N Times in Python

    Table of ContentsUsing the * operatorUsing the numpy.repeat() functionUsing the list comprehension techniqueUsing the itertools.repeat() functionConclusion This tutorial will demonstrate how to repeat list n times in Python. Using the * operator To repeat list n times in Python, use the * operator. Star operator(*) is used to multiply list by number e.g. lst*3 and […]

  • 26 December

    Get First Day of Next Month in Python

    Table of ContentsGet First Day of Next Month in PythonUsing the datetime.replace() with datetime.timedelta() functionUsing the calendar.monthrange() functionUsing the dateutil.relativedelta objectConclusion Get First Day of Next Month in Python This tutorial will demonstrate how to get first day of next month in Python. Using the datetime.replace() with datetime.timedelta() function To get first day of next […]

  • 25 December

    Convert String Array to Int Array in Python

    Table of ContentsUsing the for loop with int() functionUsing for loop with eval() functionUsing the map() with list() functionConclusion This tutorial will demonstrate how to convert string array to int array in Python. Using the for loop with int() function To convert string array to int array in Python: Use the for loop to loop […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.