如何在python中取数字的第n个数字 [英] How to take the nth digit of a number in python
本文介绍了如何在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])
这篇关于如何在python中取数字的第n个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文