python 中进制转换及format(),int()函数用法
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.
python 中进制转换及format(),int()函数用法的更多相关文章
- Python中进制转换函数的使用
Python中进制转换函数的使用 关于Python中几个进制转换的函数使用方法,做一个简单的使用方法的介绍,我们常用的进制转换函数常用的就是int()(其他进制转换到十进制).bin()(十进制转换到 ...
- python中进制转换
使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: bin(x)Convert an integer numb ...
- python实现进制转换(二、八、十六进制;十进制)
python实现进制转换(二.八.十六进制:十进制) (一)十进制整数转为二.八.十六进制 1.format实现转换>>> format(2,"b") # (10 ...
- python 实现进制转换(二进制转十进制)
摘自https://baike.baidu.com/item/%E5%8D%81%E8%BF%9B%E5%88%B6%E8%BD%AC%E4%BA%8C%E8%BF%9B%E5%88%B6 pytho ...
- python任意进制转换
python任意进制转换 import string def module_n_converter(q, s, base=None): """ 将自然数按照给定的字符串转 ...
- python中进制之间的转换
参考于:http://www.360doc.com/content/14/0428/11/16044571_372866302.shtml 在此非常感谢! ~~~~~~~~~~~~~~~~~~~~~ ...
- C++中进制转换问题
一直在刷题的时候,都会遇到一个坑,就是进制转换的问题.而每一次都傻乎乎的自己去实现一个.所以算是对以前的坑的一个总结. itoa 函数 itoa是广泛应用的非标准C语言和C++语言扩展函数.由于它不是 ...
- python的进制转换
转载于:https://www.cnblogs.com/FWF1944/p/11132409.html(方法论190404) Python整数能够以十六进制,八进制和二进制来编写,作为一般以10位基数 ...
- Python 各种进制转换
#coding=gbk var=input("请输入十六进制数:") b=bin(int(var,16)) print(b[2:]) 详细请参考python自带int函数.bin函 ...
随机推荐
- python_传递任意数量的实参
'''def name(*args): #python创建一个空元组,将收到的所有值都封装在这个元组中 """打印所有姓名""" for i ...
- React基础知识点全解
• propTypes.defaultProps 作为 properties 定义,也可以在组件外部通过键值对方式进行设置. • 设置组件初始的 state不支持 getIniti ...
- shiro + maven 的web配置(不整合spring)
本文采用的是1.4.0版本的shiro 官方中说的1.2之前,和之后的shiro配置分别为: 1.2之前: <filter> <filter-name>iniShiroFilt ...
- 在Html5中与服务器交互
转自原文 在Html5中与服务器交互 刚刚涉足职场,上头就要我研究HTML5,内嵌到手机上,这对我来说完全是一个陌生的领域,不过也正好给自己一个机会来学习,最近做到要跟服务器交互这部分,这部分可是卡了 ...
- nodejs 中使用 ftp
转自原文 nodejs 中使用 ftp 1. npm install ftp 项目 https://github.com/mscdex/node-ftp 2. 转自 http://www.open ...
- POJ 1430
上面的估计是题解吧....呃,如果真要用到公式的话,确实没听过.... #include <iostream> #include <cstdio> #include <a ...
- linux下使用DBCA(database configuration assistant)创建oracle数据库
前提:切换到图形界面 到Oracle的bin文件夹下,使用oracle用户.运行dbca就可以.和windows的效果一样. 假设出现乱码 export LANG="en_US:UTF-8& ...
- UVALive 4192/HDU 2959 Close Enough Computations 数学
Close Enough Computations Problem Description The nutritional food label has become ubiquitous. A sa ...
- python解压,压缩,以及存数据库的相关操作
zipfile实现压缩整个目录和子目录 import os,shutil,zipfile,glob def dfs_get_zip_file(input_path,result): # files = ...
- CentOS7安装第三方yum源EPEL
转自:https://blog.csdn.net/u012208775/article/details/78784616 一.简介 EPEL是企业版 Linux 附加软件包的简称,EPEL是一个由Fe ...