Python数字,字符格式转换笔记

好记性不如烂笔头,有一些常用的数字,字符串转换的学习笔记

1. 将二进制数转换为指定格式的字符串形式

先来看一小段代码

>>> a = 0b0100
>>> print a
4
>>> print format(a, ‘#0b‘)
0b100
>>> print format(a, ‘0b‘)
100
>>> print format(a, ‘04b‘)
0100
>>> print format(a, ‘#06b‘)  # 注意字符串中的0b也是要占两个宽度的,所以这里宽度是6才会补0
0b0100

变量a的二进制是0100,直接print的a是十进制4。如果想要将a转换为二进制形式的字符串,需要用到format函数。

官方的文档:https://docs.python.org/2/library/string.html#format-specification-mini-language

  • format函数中的格式说明如下

‘#‘ -> 只适用于数字,指明转换后的格式包含前缀(如0b, 0o, 0x)

The ‘#‘ option is only valid for integers, and only for binary, octal, or
hexadecimal output.  If present, it specifies that the output will be prefixed
by ‘0b‘, ‘0o‘, or ‘0x‘, respectively.

0   -> 代表当数字转换成字符后,宽度如果小于指定的宽度,则前面补0

  • 同样的,不用format函数,可以用另外一种方法来实现和上面相同的效果
>>> a = 0b0100
>>> print "a = {0:04b}".format(a)
a = 0100
>>> print "a = {0:#06b}".format(a)
a = 0b0100

2. 将字符串(不同进制的)或者数字(不同进制的,比如2进制、16进制)转换成十进制数字

int函数可以达到这个要求,来看看一小段代码

>>> int(0b001)  # ref 1
4
>>> int(0xff)   # ref 2
255
>>> int(‘0b1010‘, 2)  # ref 3
10
>>> int(‘111‘, 2)  # ref 4
7
>>> int(‘111‘)  # ref 5
111

int函数中的第一个参数,可以传入数字型,也可以传入字符型。

  • 当传入数字型时,会根据类型,自动将其转换为10进制。【参考 ref 1,2】
  • 当传入是字符时,可以再指定第二个参数,用来表明第一个字符串是什么进制,然后int再将其转换为10进制。当不指定第二个参数(进制类型)的时候,将默认把第一个字符串当作是10进制处理。【参考 ref 3, 4, 5】
>>> help(int)  # 更多的信息参考下面的文档

class int(object)
 |  int(x=0) -> int or long
 |  int(x, base=10) -> int or long
 |
 |  Convert a number or string to an integer, or return 0 if no arguments
 |  are given.  If x is floating point, the conversion truncates towards zero.
 |  If x is outside the integer range, the function returns a long instead.
 |
 |  If x is not a number or if base is given, then x must be a string or
 |  Unicode object representing an integer literal in the given base.  The
 |  literal can be preceded by ‘+‘ or ‘-‘ and be surrounded by whitespace.
 |  The base defaults to 10.  Valid bases are 0 and 2-36.  Base 0 means to
 |  interpret the base from the string as an integer literal.
 |  >>> int(‘0b100‘, base=0)
 |  4

3. 将不同进制的数字,转换成二进制形式的字符串

bin函数可以达到这个要求,来看看一小段代码

bin函数只接受数字型的参数

>>> bin(0b0100)
‘0b0100‘
>>> bin(0xff)
‘0b11111111‘

4. 将不同进制的数字,转换成十六进制的字符串

同样的,hex函数可以帮助我们完成,来看一小段代码

hex函数也只接受数字型的参数

>>> hex(15)
‘0xf‘
>>> hex(0b111)
‘0x7‘

5. 还有一个小伙伴,oct函数,用来干嘛的,类似上面的bin和hex

>>> oct(8)
‘010‘
>>> hex(7)
‘07‘

That‘s it!

时间: 2024-07-29 07:39:28

Python数字,字符格式转换笔记的相关文章

python基础 字符编码转换

python2 1 #python2上所有的字符编码都需要先decode到unicode,再从unicode encode到目标编码 2 str_utf8 = "我就是我" 3 print("str_utf-8:我就是我:",str_utf8) 4 #将utf-8转换为unicode 5 str_utf8_to_unicode = str_utf8.decode("utf-8") 6 print(str_utf8_to_unicode) 7 #将

使用python将ppm格式转换成jpg【转】

转自:http://blog.csdn.net/hitbeauty/article/details/48465017 最近有个很火的文章,叫 有没有一段代码,让你觉得人类的智慧也可以璀璨无比? 自己试了一下里面的代码,可是图片格式让我犯难了.PPM格式的图片怎么打开呢?难不成还特意为它去装个专业的图形处理软件吗? 关键是笔记本不允许装盗版软件啊! 研究了一下,用Python可以很容易就把PPM转换成jpg格式啊,代码如下: #coding=utf-8 import Image img = Ima

python 数字和字符串转换问题

一.python中字符串转换成数字 (1)import string tt='555' ts=string.atoi(tt) ts即为tt转换成的数字 转换为浮点数 string.atof(tt) (2)直接int int(tt)即可. 二.数字转换成字符串 tt=322 tem='%d' %tt tem即为tt转换成的字符串

python 使用json格式转换

什么是json: JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集.JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等).这

字符编码转换笔记

何为字符编码? 字符编码为计算机文字的存储格式, 例如 英文 字母 以ASCII编码存储, 即单字节存储,  其他字符编码有 UTF-8(通用字符编码格式), 其他区域性编码格式, 例如 ISO-8859(西欧), windows-1251俄文,中文GB编码. 为什么需要转换? 正因各个地区有不同的编码格式, 为了交换信息的目的, 就需要将相同字符的 从一种编码格式 转换为 另外一种编码格式. 通用的编码格式为 UTF-8, 其囊括了 世界上所有字符, 所以一般为了通用性, 文件都以UTF-8编

python中字符的转换

在Python的类型层次中,有3种不同的字符串类型:"unicode",表示Unicode字符串(文本字符串)."str",表示字节字符串(二进制数据):"basestring".表示前两种字符串类型的父类. fat_phone = request.GET['phone'] 该类型为unicode类型[fat_phone] 输出为 [u'13217788111']print [fat_phone.encode("utf-8")

ANIS与UNICODE字符格式转换:MultiByteToWideChar() 和WideCharToMultiByte() 函数

资料来自: http://blog.csdn.net/holamirai/article/details/47948745 http://www.cnblogs.com/wanghao111/archive/2009/05/25/1489021.html ---------------------------------------------------------------------------------------------------------------- 使用MultiBy

Python之图片格式转换

import os import shutil from PIL import Image def getAllFiles(dirName, houzhui=' '): results = [] for file in os.listdir(dirName): file_path = os.path.join(dirName, file) if houzhui == ' ': if os.path.isfile(file_path): results.append([file_path, os.

windows下python+ffmpeg视频格式转换

下载ffmpeg-20141204-git-5fe026f-win32-static 找到里面bin文件夹里面的ffmpeg.exe 代码 import  os os.popen3('F:/ffmpeg.exe -i F:/static/media/video/1080.avi  F:/static/media/ffvideo/1080.mov') 就可以把源文件1080.avi 转成1080.mov 纠结差不多一天,都不知道从哪里下手.网上看别的开发语言的各种引用什么的把我完全绕晕了 . 原来