• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Using Python to Split a Number into Digits

Using Python to Split a Number into Digits

February 11, 2022 Leave a Comment

In Python, there are a number of ways we can split a number into digits. The easiest way to get the digits of an integer is to use list comprehension to convert the number into a string and get each element of the string.

def getDigits(num):
    return [int(x) for x in str(num)]

print(getDigits(100))
print(getDigits(213))

#Output:
[1,0,0]
[2,1,3]

We can also get the digits of a number using a for loop in Python.

def getDigits(num):
    digits = []
    for x in str(num):
        digits.append(int(x))
    return digits

print(getDigits(100))
print(getDigits(213))

#Output:
[1,0,0]
[2,1,3]

Another way is we can define a loop to get the remainder of the number after dividing by 10, divide by 10, and then continue the process until we collect all the digits.

def getDigits(num):
    digits = []
    while num > 0:
        digits.append(num % 10)
        num = int(num/10)
    digits.reverse()
    return digits

print(getDigits(100))
print(getDigits(213))

#Output:
[1,0,0]
[2,1,3]

When working with numbers in computer programming, it is useful to be able to easily extract information from our variables.

One such piece of information is the digits that a number is made up of. In Python, we can easily split a number into its digits.

The easiest way to get the digits of an integer in Python is to use list comprehension.

Using list comprehension, we convert the number into a string and get each element of the string.

def getDigits(num):
    return [int(x) for x in str(num)]

print(getDigits(100))
print(getDigits(213))

#Output:
[1,0,0]
[2,1,3]

We can also get the digits of a number using a for loop in Python and applying the same logic as in the list comprehension code example.

def getDigits(num):
    digits = []
    for x in str(num):
        digits.append(int(x))
    return digits

print(getDigits(100))
print(getDigits(213))

#Output:
[1,0,0]
[2,1,3]

Using Division and Remainders to Split a Number into Digits in Python

We can also split a number into digits using a method without converting the number into a string.

To get the digits of a number using division, we find the remainder of the number divided by 10. Then we will divide by 10, and continue the process until we hit 0.

Doing this method in Python is easy with a while loop.

Below is an example of a function for how to split a number into digits using a loop in Python.

def getDigits(num):
    digits = []
    while num > 0:
        digits.append(num % 10)
        num = int(num/10)
    digits.reverse()
    return digits

print(getDigits(100))
print(getDigits(213))

#Output:
[1,0,0]
[2,1,3]

Hopefully this article has been useful for you to learn how to split a number into digits in Python.

Other Articles You'll Also Like:

  • 1.  Break Out of Function in Python with return Statement
  • 2.  Python if else do nothing – Using pass Statement to Do Nothing in Python
  • 3.  Get Current Year in Python
  • 4.  Remove Extension from Filename in Python
  • 5.  Initialize Multiple Variables in Python
  • 6.  Convert List into Tuple Using Python
  • 7.  Using Python to Convert Integer to String with Leading Zeros
  • 8.  Remove Trailing Zeros from String with rstrip() in Python
  • 9.  How to Group and Aggregate By Multiple Columns in Pandas
  • 10.  Check if Number is Larger than N in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy