How to Take the Nth Digit of a Number in Python

How to take the nth digit of a number in python

First treat the number like a string

number = 9876543210
number = str(number)

Then to get the first digit:

number[0]

The fourth digit:

number[3]

EDIT:

This will return the digit as a character, not as a number. To convert it back use:

int(number[0])

Finding nth digit of a number in a list

x=[[4,682,31249,81,523],[321741245,7],[349,25,5]]
x=[item for sublist in x for item in sublist]
max_len=len(str(max(x)))
x=[str(y) for y in x]
def digits(x,index):
digit_list=[]
for num in x:
try:
digit_list.append(num[index])
except:
pass
return digit_list
for index in range(0,max_len):
print(digits(x,index))

Explanation:

1.Intialize x

2.Flat x i.e convert nested list to list

3.Calculate the length of highest number. You can take 'n' on your choice as well. I took it as length of highest number

4.Convert all numbers as string

5.define function digits(). It initializes a list & append single digits according the index called

6.By looping over range(0,max_len/n), call digits() for each index:0-->n

output

How can i return the Nth digit of a list in python?

x = [1,4,5,10,23,2,5,7,19]

def func(x, n):
return ''.join(map(str, x))[n-1]

print(func(x, 7))

Prints:

3

Insert a digit after the nth character/number to form a longer integer

You can split the number in strings, add the number you need as a string and convert back to int

id = int(str(line[:6]) + "19" + str(line[6:15]))
And you don't need an additional function for this.

PI nth digit after decimal

The float datatype has a limit on how precise you can get. 50 digits is around that.

I recommend using a Decimal instead to represent numbers with such high precision:

>>> from decimal import Decimal
>>> d = Decimal('3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270938521105559644622948954930381964428')
>>> format(d, ".50f")
'3.14159265358979323846264338327950288419716939937511'
>>> format(d, ".204f")
'3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102709385211055596446229489549303819644280'

This way you can do math on it without losing precision, which "treating it as a string" doesn't let you do.

How to find the nth digit in a list in python

Lists work in an index form. So index[0] of a list is the first item. So to find the nth item in a list, your code would look something like this.

list = [a, b, c, d, e, f, g]
chosen_number = input("Which number would you like from 1-7?")
print("The item {} places into the list is {}".format(chosen_number, list[chosen_number-1]))

Hopefully this helps. This doesn't account for errors that might occur when the user is typing in e.g. if they type in a word.

How to check a specific digit in integer Python

A more optimized (though slightly uglier) approach would be to iterate over the integer and use division along with the modulus to check each digit:

import math

def check_number(integer: int, digit: int, place: int) -> bool:
while place > 1:
integer = math.floor(integer / 10)
place -= 1

if integer == 0:
return False

if integer % 10 == digit:
return True
else:
return False

print(check_number(12345, 3, 3)) # True
print(check_number(12345, 1, 5)) # True
print(check_number(12345, 4, 1)) # False

In the above in order to "queue up" the digit to be checked, we iterate in a while loop and divide the input by 10 however many times is required to put the digit to be examined in place into the tens position. Then we check that value using the modulus. Should the input not have a digit in that place, we return false, and likewise we return false should the digit exist but not match the input.

Note that in general the above solution is preferable to a string based solution, because the overhead in creating and manipulating a string is fairly high, much higher than doing simple division of the input integer.



Related Topics



Leave a reply



Submit