You can do it with integer division and remainder methods

def get_digit(number, n):
    return number // 10**n % 10

get_digit(987654321, 0)
# 1

get_digit(987654321, 5)
# 6

The // performs integer division by a power of ten to move the digit to the ones position, then the % gets the remainder after division by 10. Note that the numbering in this scheme uses zero-indexing and starts from the right side of the number.

Answer from Chris Mueller on Stack Overflow
🌐
Medium
medium.com › @alopezmartinez › extracting-digits-from-an-integer-in-python-a-step-by-step-guide-96c9ff98db37
Extracting Digits from an Integer in Python: A Step-by-Step Guide | by AmericaML | Medium
August 6, 2024 - Purpose of the Formula: The formula (number % 10**4) // 10**3 allows us to extract the leftmost digit of a number by isolating the relevant digits and then shifting them down to get the desired value.
Discussions

python - How to extract digits from a number from left to right? - Stack Overflow
Note: By replacing everywhere 10 by any positive integer b, you get the digits of n in base b. ... Convert number to string,reverse and iterate through it. More on stackoverflow.com
🌐 stackoverflow.com
checking the value of nth digit of a number

You can target specific digits using modulo and floor division.

myNumber = 1234567
digits12 = myNumber / 100000          # 12
digit3 = (myNumber / 10000) % 10    # 3
digit4 = (myNumber / 1000) % 10      # 4

That way you don't need to change data types (like if you changed myNumber to a string, then used slicing to get the digits you want and turning them back into ints)

Edit: sorry, didn't realize you were using Python 2, where / is floor division.

More on reddit.com
🌐 r/learnpython
13
4
January 12, 2014
python - How to find length of digits in an integer? - Stack Overflow
It is crazy how we can get so caught up on the fastest smart solution. There aren't even that many if checks, it massively improves everything, it is not ugly, yet this is not the favorite solution. If we are worrying about number accuracy we wouldn't be using python integers to begin with. It's not even that much code! We should be making things easier AND simpler. Thank you for reminding me of that Captain'Flam :D 2023-07-20T05:01:18.87Z+00:00 ... def num_digits... More on stackoverflow.com
🌐 stackoverflow.com
How to split int into digits?
x = 19 digits = str(x) And maybe that's enough, because a string is a sequence which can be indexed, sliced, looped. But if you need it to be a list then you could do digits = list(str(x)) instead. Or maybe you want integers for the items in the list? Then it'd have to be like digits = [int(digit) for digit in str(x)] More on reddit.com
🌐 r/learnpython
5
4
January 22, 2023
🌐
PYnative
pynative.com › home › python › programs and examples › count digits of a number in python
Python Count Digits of a Number [9 Ways] – PYnative
March 27, 2025 - import math num = 123456 count = 0 if num == 0: count = 1 # Special case for zero, which has one digit else: count = math.floor(math.log10(abs(num))) + 1 print("Number of digits:", count) # Output: # Number of digits: 6Code language: Python (python) Run · The logarithm base 10 of a number tells you how many times 10 must be multiplied to get the number.
🌐
Tutorial Reference
tutorialreference.com › home › python › examples › faq › how to get ones, tens, hundreds, thousands digits of a number in python
How to Get Ones, Tens, Hundreds, Thousands Digits of a Number in Python | Tutorial Reference
Reversing the final list of digits is often conceptually clearer. This method repeatedly extracts the last digit using the modulo operator and then removes that digit using floor division until the number becomes zero. def get_digits_while(num): """Gets digits using iterative division (returns ...
🌐
Code and Debug
codeanddebug.in › home › data structures & algorithms › extraction of digits in python | explained step-by-step
Extraction of Digits in Python | Explained Step-by-Step
July 7, 2025 - Isolate the last digit of the number by using the modulus operation (% 10) ... This works because the modulus operation gives the remainder when the number is divided by 10, which is effectively the last digit of the number.
Find elsewhere
🌐
AskPython
askpython.com › python › string › extract-digits-from-python-string
2 Easy Ways to Extract Digits from a Python String - AskPython
May 4, 2026 - For example, from “Order-1042-abc” you would get the number 1042. Python gives you two direct tools for this: str.isdigit() to test individual characters, and the re module to match digit patterns across the whole string.
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "python tutorial: how-to guides for everyday python problems" › "how to split an integer into digits in python"
How to Split an Integer Into Digits in Python | Delft Stack
February 14, 2024 - A list comprehension is the most direct choice when the input is an integer and the desired result is a list of digits in their usual left-to-right order.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-extract-digits-from-given-string
Python - Extract digits from given string - GeeksforGeeks
July 11, 2025 - ... import re s = "abc123def456gh789" # Extract digits using regex digits = ''.join(re.findall(r'\d', s)) print(digits) ... re.findall() function is used to find all digits (\d) in the string, returning them as a list of individual characters.
🌐
YouTube
youtube.com › watch
How To extract the digits of a number in Python - YouTube
In this video we will learn how to extract the digits of a number, combine modulo and division in Python. Please Subscribe to Asim Code.Code: https://github...
Published: April 24, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › program-count-digits-integer-3-different-methods
Count digits - GeeksforGeeks
The idea is to remove digits from right by calling a recursive function for each digit. The base condition of this recursive approach is when we divide the number by 10 and the number gets reduced to 0, so return 1 for this operation.
Published: 3 weeks ago
🌐
Programiz
programiz.com › python-programming › examples › number-of-digits
Python Program to Count the Number of Digits Present In a Number | Programiz
March 14, 2024 - To understand this example, you should have the knowledge of the following Python programming topics: ... In this program, the while loop is iterated until the test expression num != 0 is evaluated to 0 (false). After the first iteration, num will be divided by 10 and its value will be 345. Then, the count is incremented to 1. After the second iteration, the value of num will be 34 and the count is incremented to 2. After the third iteration, the value of num will be 3 and the count is incremented to 3.
🌐
DEV Community
dev.to › jabermudez11 › splitting-a-number-into-individual-digits-python-3g8a
Splitting a Number into Individual Digits Python - DEV Community
December 6, 2020 - If you are looking only for the code then here it is number = 12345 numList = [int(digit) for digi... Tagged with python, beginners.
🌐
Tutorial Reference
tutorialreference.com › home › python › examples › faq › how to extract digits from numbers and strings in python
How to Extract Digits from Numbers and Strings in Python | Tutorial Reference
March 29, 2025 - The simplest way to get the first digit of an integer is to convert it to a string and access the first character: num = 246810 first = int(str(num)[0]) # Convert to string, get first char, convert back to int print(first) # Output: 2 · The str() function transforms the integer into a string, ...
🌐
Vultr
docs.vultr.com › python › examples › count the number of digits present in a number
Python Program to Count the Number of Digits Present In a Number | Vultr Docs
September 27, 2024 - In this method, each division by 10 removes one digit from the number, and the count variable keeps track of how many digits have been processed. Import the math module. Calculate the logarithm base 10 of the number.
🌐
Reddit
reddit.com › r/learnpython › checking the value of nth digit of a number
r/learnpython on Reddit: checking the value of nth digit of a number
January 12, 2014 -

Hello everyone,

First some background to the problem I am trying to solve. I have a bunch of 7 digit integers and I need to sort them based on the value of the first 4 digits. For example, I need to classify the number if the first 2 digits are between 44 and 59 and the third and fourth digits are between 4 and 7 (inclusive) as area A.

So far I have simply dumped all of my numbers into a list and sorted them. I am thinking that I need to make a definition with some for loops in them but I am not sure how to check the values of the digits of the numbers. Any ideas of how to check these values?

I hope I explained this so that everyone can understand it.

Side note I am using window 8 using python 2.7.6

Thanks in advance for the help

Edit: Just FYI I am very new to Python, I only start learning it in my spare time about a month ago.

🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-for-sum-the-digits-of-a-given-number
Sum the Digits of a Given Number - Python - GeeksforGeeks
July 15, 2025 - For example, given the number 12345, the sum of its digits is 1 + 2 + 3 + 4 + 5 = 15. This method efficiently extracts each digit using the modulus (%) and integer division (//) operations, avoiding unnecessary string conversions. It is the fastest and most memory-efficient approach, making it ideal for performance-focused applications. ... n = 12345 sum = 0 while n > 0: sum += n % 10 # extract last digit n //= 10 # remove last digit print(sum)
🌐
Bobby Hadz
bobbyhadz.com › blog › python-split-integer-into-digits
How to Split an Integer into Digits in Python | bobbyhadz
April 8, 2024 - Copied!import math an_int = 13579 x = math.log(an_int, 10) y = math.ceil(x) list_of_digits = [(an_int//(10**i)) % 10 for i in range(y, -1, -1) ][bool(math.log(an_int, 10) % 1):] print(list_of_digits) # [1, 3, 5, 7, 9] ... The math.ceil() method ...