python 中进制转换及format(),int()函数用法「建议收藏」
python,进制,转换,format,int,函数,用法,建议,收藏
2025-04-07 09:01:29 时间
大家好,又见面了,我是你们的朋友全栈君。
python中数值型变量好像只能是十进制形式表示,其他类型变量只能以字符串形式存在,可以通过format函数将int类型变量转换成其他进制字符串,如下所示:
v_code=15
# 2进制
x=format(v_code, '#b') # '0b1111' 等效于:x=bin(v_code)
y=format(v_code, 'b') # '1111'
# 8进制
x=format(v_code, '#o') # '0o17', 等效于:x=oct(v_code)
y=format(v_code, 'o') # '17'
# 16进制
x=format(v_code, '#x') # '0xf', 等效于:x=hex(v_code)
y=format(v_code, 'x') # 'f'
z=format(v_code, '#X') # 'OXF'
z=format(v_code, 'X') # 'F'
其中,通过格式符#决定是否显示前置符号,通过f和F决定16进制中字符的大小写。
将其他进制字符串转换成10进制数,用到函数int,如下:
z='F'
x=int(z,16) #将16进制字符串转换为int值
其中进制可选2,10,8,16,而缺省时为10.
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/156130.html原文链接:https://javaforall.cn
相关文章
- 2021年国庆你的朋友去哪浪了?让Python告诉你!
- Python项目44-前后端分离项目(前戏)
- Python十大装B语法「建议收藏」
- 如何在 Python 中使用断点调试
- Python的抽象魔法--类
- Python项目51-课程页面
- python计算最大公约数和最小公倍数_python怎么求最大公约数和最小公倍数
- Python 基于 TCP 传输协议的网络通信实现
- python识别文字位置_如何利用Python识别图片中的文字
- python三种基本数据类型有哪些_python中有哪些基本数据类型
- python copy&deepcopy
- python dll注入 网络_dll注入
- python button使用方法_python gui界面设计
- Python知识点(史上最全)
- python批量采集美团网餐饮商家评论信息
- Python进制转换与ASCII转换
- mt4 python_一个使用Python自动化交易外汇MT4脚本实现「建议收藏」
- Python 更换国内pip源
- python十进制转换_Python 进制转换
- matlab wavedec2 函数,python小波变换 wavedec2函数 各个返回值详解