How to Split a Number into Digits in Python?

In this tutorial, I will help you learn how to split a number into digits using Python. As a Python developer, I encountered a situation to split numbers into digits while working with the individual digits of a number. Let’s get into the various methods that you can use to achieve this with detailed examples.

Split a Number into Digits in Python

Let’s consider a real-life scenario. Suppose you have a customer database for your e-commerce store in the United States. Each customer record includes a phone number stored as an integer. However, you realize that you need to process each digit of the phone number separately for a specific feature you’re implementing.

For example, you may have a phone number like 2125551234. Your goal is to split this number into a list of individual digits: [2, 1, 2, 5, 5, 5, 1, 2, 3, 4].

Read How to Create a Square Function in Python?

Approach 1: Convert the Number to a String

One simple approach to split a number into digits is to convert the number to a string and then access each character individually. Here’s how you can do it:

phone_number = 2125551234
digit_list = [int(digit) for digit in str(phone_number)]
print(digit_list)

Output:

[2, 1, 2, 5, 5, 5, 1, 2, 3, 4]

I have executed the above example code and added the screenshot below.

Split a Number into Digits in Python

In this example, we first convert the phone_number to a string using the str() function. Then, we use a list comprehension to iterate over each character of the string and convert it back to an integer using int(). The resulting list digit_list contains the individual digits of the phone number.

check out __new__ vs __init__ in Python

Approach 2: Use the Modulo Operator and Floor Division

Another approach to split a number into digits is by using the modulo operator (%) and floor division (//). This method repeatedly divides the number by 10 and extracts the remainder as the digit. Here’s an example:

def split_number(number):
    digits = []
    while number > 0:
        digit = number % 10
        digits.insert(0, digit)
        number //= 10
    return digits

zip_code = 10001
digit_list = split_number(zip_code)
print(digit_list)

Output:

[1, 0, 0, 0, 1]

I have executed the above example code and added the screenshot below.

How to Split a Number into Digits in Python

In this approach, we define a function called split_number() that takes a number as input. We initialize an empty list called digits to store the individual digits.

Inside the function, we start a loop that continues as long as number is greater than 0. In each iteration, we use the modulo operator to extract the rightmost digit by calculating number % 10. We then insert this digit at the beginning of the digits list using the insert() method with index 0.

Next, we update the number by performing floor division (//) by 10, effectively removing the rightmost digit. The loop continues until all digits have been processed. Finally, we return the digits list containing the individual digits of the number.

Read How to Implement the Sigmoid Activation Function in Python?

Split Numbers with a Specific Number of Digits

In some cases, you may need to split a number into groups of digits with a specific length. For example, splitting a credit card number into groups of four digits. You can achieve this using list comprehension and string slicing:

credit_card = 1234567890123456
group_size = 4
digit_groups = [int(str(credit_card)[i:i+group_size]) for i in range(0, len(str(credit_card)), group_size)]
print(digit_groups)

Output:

[1234, 5678, 9012, 3456]

I have executed the above example code and added the screenshot below.

Split a Number into Digits in Python with a Specific Number of Digits

In this example, we have a credit_card number and a group_size that determines the number of digits in each group. We convert the credit card number to a string and use list comprehension to slice the string into groups of group_size digits. The range() function is used to generate the starting indices for each group, incrementing group_size in each iteration. We convert each group back to an integer and store them in the digit_groups list.

Check out How to Use the insert() Function in Python?

Conclusion

In this tutorial, I have explained how to split a number into digits using Python. I explored two approaches to accomplish this task: converting the number to a string and using the modulo operator with floor division. We also saw how to split numbers with a specific number of digits.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Letโ€™s be friends

Be the first to know about sales and special discounts.