如何在python中取数字的第n位
- 作者: 吹吹风777
- 来源: 51数据库
- 2023-01-30
问题描述
我想从 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 中做类似的事情?是不是应该先改成string再改成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?
推荐答案
先把数字当作字符串处理
First treat the number like a string
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])
推荐阅读
热点文章
Discord.py(重写)on_member_update 无法正常工作
0
Discord.py 在 vc 中获取用户分钟数
0
discord.py 重写 |为我的命令出错
0
Discord.py rewrite 如何 DM 命令?
0
播放音频时,最后一部分被切断.如何解决这个问题?(discord.py)
0
在消息删除消息 Discord.py
0
如何使 discord.py 机器人私人/直接消息不是作者的人?
0
(Discord.py) 如何获取整个嵌入内容?
0
Discord bot 尽管获得了许可,但不能提及所有人
0
Discord.py discord.NotFound 异常
0
