How do you get digits from an integer in python?

I'm very sorry for necro-threading but I wanted to provide a solution without converting the integer to a string. Also I wanted to work with more computer-like thinking so that's why the answer from Chris Mueller wasn't good enough for me.

So without further ado,

import math

def count_number(number):
    counter = 0
    counter_number = number
    while counter_number > 0:
        counter_number //= 10
        counter += 1
    return counter


def digit_selector(number, selected_digit, total):
    total_counter = total
    calculated_select = total_counter - selected_digit
    number_selected = int(number / math.pow(10, calculated_select))
    while number_selected > 10:
        number_selected -= 10
    return number_selected


def main():
    x = 1548731588
    total_digits = count_number(x)
    digit_2 = digit_selector(x, 2, total_digits)
    return print(digit_2)


if __name__ == '__main__':
    main()

which will print:

5

Hopefully someone else might need this specific kind of code. Would love to have feedback on this aswell!

This should find any digit in a integer.

Flaws:

Works pretty ok but if you use this for long numbers then it'll take more and more time. I think that it would be possible to see if there are multiple thousands etc and then substract those from number_selected but that's maybe for another time ;)

Usage:

You need every line from 1-21. Then you can call first count_number to make it count your integer.

x = 1548731588
total_digits = count_number(x)

Then read/use the digit_selector function as follows:

digit_selector('insert your integer here', 'which digit do you want to have? (starting from the most left digit as 1)', 'How many digits are there in total?')

If we have 1234567890, and we need 4 selected, that is the 4th digit counting from left so we type '4'.

We know how many digits there are due to using total_digits. So that's pretty easy.

Hope that explains everything!

Han

PS: Special thanks for CodeVsColor for providing the count_number function. I used this link: https://www.codevscolor.com/count-number-digits-number-python to help me make the digit_selector work.

  1. HowTo
  2. Python How-To's
  3. Find Number of Digits in a Number in Python

Created: June-07, 2021 | Updated: August-10, 2021

  1. Find the Number of Digits Inside a Number With the math.log10() Function in Python
  2. Find the Number of Digits Inside a Number With the len() Function in Python

This tutorial will introduce the methods to count the number of digits inside a number in Python.

Find the Number of Digits Inside a Number With the math.log10() Function in Python

The math.log10() function inside the math module of Python is used to find the log of base 10 for any specified number. As the integers are also in base 10, we can get the number of digits inside the specified integer with this method.

The following code snippet shows us how we can find the number of digits inside a number with the math.log10() function.

import math
n = -10
if n > 0:
    digits = int(math.log10(n))+1
elif n == 0:
    digits = 1
elif n < 0:
    digits = int(math.log10(-n))+2
print(digits)

Output:

3

We calculated the number of digits inside the number -10 with the math.log10() function in the code above. This code also handles the case where the number is 0, as the log of 0 cannot be calculated. We first check whether the number is greater than 0. If the number is greater than 0, we calculate the digits by taking the log and adding 1 to the result. This process is done because the log of any number is 1 less than the number of digits inside that number.

If the number is equal to 0, we set the digits equal to 1. If the number is less than 0, we calculate the number of digits by taking the log of the additive inverse of that negative number and adding 2 to the result. In the case of negative numbers, we add an extra 1 because we consider the - sign as a digit in this example. In the end, we print the number of digits on the screen.

This method is great to use if we want to determine the number of digits inside an integer. However, it doesn’t work with decimal numbers or any numbers with a floating decimal point.

Find the Number of Digits Inside a Number With the len() Function in Python

The len() function is a built-in function in Python used to calculate the number of characters inside a string variable. The len() function takes a string as an input parameter and returns the number of characters inside that string. To calculate the number of digits inside a number using the len() function, we first have to convert that number into a string with the str() function.

The str() function is also a built-in function in Python used to convert objects of different types into a string variable. The following code snippet shows us how we can find the number of digits inside a number with the len() function:

n = -100.90
digits = len(str(n))
print(digits)

Output:

6

We calculated the number of digits inside the number -100.90 with the len() function in the code above. First, we converted the number into a string using the str() function. After that, we passed the resultant string to the len() function and stored the values returned by the len() function inside the digits variable. In the end, we printed the value inside the digits variable. The output clearly shows that this approach is also applicable for decimal numbers or numbers containing a floating decimal point.

The len() method is far superior to the math.log10() method for finding the number of digits inside a decimal number in Python. The reason is the len() method is clear, concise, and also handles floating decimal points unlike the math.log10() method, which is unnecessarily complex and doesn’t handle floating decimal points.

Related Article - Python Number

  • Create a List of Odd Numbers in Python
  • Convert Letter to Number in Python
  • Display a Number With Leading Zeros in Python
  • Check if a Character Is a Number in Python
  • How do you get digits of a number in Python?

    int(str(number)[i-1]) . Or if you need to handle all digits: for index, digit in enumerate(str(number), start=1): digit = int(digit) ... .
    Stack Overflow is not a code-writing or tutorial service..

    How do you convert integers to digits?

    Just use % 10 to get the last digit and then divide your int by 10 to get to the next one. int temp = test; ArrayList<Integer> array = new ArrayList<Integer>(); do{ array. add(temp % 10); temp /= 10; } while (temp > 0); This will leave you with ArrayList containing your digits in reverse order.

    How do you split a number into digits in Python?

    To split an integer into digits: Use the str() class to convert the integer to a string. Use a for loop to iterate over the string. Use the int() class to convert each substring to an integer and append them to a list.

    How do you convert integers to numbers in Python?

    To convert, or cast, a string to an integer in Python, you use the int() built-in function. The function takes in as a parameter the initial string you want to convert, and returns the integer equivalent of the value you passed. The general syntax looks something like this: int("str") .