如何在python中取数字的第n个数字
问题描述:
我想从python中的N位数字中提取第n位数字.例如:
I want to take the nth digit from an N digit number in python. For example:
number = 9876543210
i = 4
number[i] # should return 6
如何在python中做类似的事情?我应该先将其更改为字符串,然后再将其更改为int进行计算吗?
How can I do something like that in python? Should I change it to string first and then change it to int for the calculation?
答
首先将数字视为字符串
number = 9876543210
number = str(number)
然后获得第一个数字:
number[0]
第四位数字:
number[3]
这将以字符而不是数字的形式返回数字.转换回使用:
This will return the digit as a character, not as a number. To convert it back use:
int(number[0])