python开发_类型转换convert

在python的开发过程中,难免会遇到类型转换,这里给出常见的类型转换demo:

   int(x [,base ])         将x转换为一个整数
   long(x [,base ])        将x转换为一个长整数
   float(x )               将x转换到一个浮点数
   complex(real [,imag ])  创建一个复数
   str(x )                 将对象 x 转换为字符串
   repr(x )                将对象 x 转换为表达式字符串
   eval(str )              用来计算在字符串中的有效Python表达式,并返回一个对象
   tuple(s )               将序列 s 转换为一个元组
   list(s )                将序列 s 转换为一个列表
   chr(x )                 将一个整数转换为一个字符
   unichr(x )              将一个整数转换为Unicode字符
   ord(x )                 将一个字符转换为它的整数值
   hex(x )                 将一个整数转换为一个十六进制字符串
   oct(x )                 将一个整数转换为一个八进制字符串

下面是我做的demo:

 1 #类型转换
 2 #convert
 3 
 4 #convert to int
 5 print('int()默认情况下为:', int())
 6 print('str字符型转换为int:', int('010'))
 7 print('float浮点型转换为int:', int(234.23))
 8 #十进制数10,对应的2进制,8进制,10进制,16进制分别是:1010,12,10,0xa
 9 print('int(\'0xa\', 16) = ', int('0xa', 16))
10 print('int(\'10\', 10) = ', int('10', 10))
11 print('int(\'12\', 8) = ', int('12', 8))
12 print('int(\'1010\', 2) = ', int('1010', 2))
13 
14 #convert to long
15 print('int浮点型转换为int:', int(23))
16 
17 #convert to float
18 print('float()默认情况下为:', float())
19 print('str字符型转换为float:', float('123.01'))
20 print('int浮点型转换为float:', float(32))
21 
22 #covert to complex
23 print('创建一个复数(实部+虚部):', complex(12, 43))
24 print('创建一个复数(实部+虚部):', complex(12))
25 
26 #convert to str
27 print('str()默认情况下为:', str())
28 print('float字符型转换为str:', str(232.33))
29 print('int浮点型转换为str:', str(32))
30 lists = ['a', 'b', 'e', 'c', 'd', 'a']
31 print('列表list转换为str:', ''.join(lists))
32 
33 #covert to list
34 strs = 'hongten'
35 print('序列strs转换为list:', list(strs))
36 
37 #covert to tuple 
38 print('列表list转换为tuple:', tuple(lists))
39 
40 #字符和整数之间的转换
41 #char coverted to int
42 print('整数转换为字符chr:', chr(67))
43 print('字符chr转换为整数:', ord('C'))
44 
45 print('整数转16进制数:', hex(12))
46 print('整数转8进制数:', oct(12))

运行效果:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
int()默认情况下为: 0
str字符型转换为int: 10
float浮点型转换为int: 234
int('0xa', 16) =  10
int('10', 10) =  10
int('12', 8) =  10
int('1010', 2) =  10
int浮点型转换为int: 23
float()默认情况下为: 0.0
str字符型转换为float: 123.01
int浮点型转换为float: 32.0
创建一个复数(实部+虚部): (12+43j)
创建一个复数(实部+虚部): (12+0j)
str()默认情况下为: 
float字符型转换为str: 232.33
int浮点型转换为str: 32
列表list转换为str: abecda
序列strs转换为list: ['h', 'o', 'n', 'g', 't', 'e', 'n']
列表list转换为tuple: ('a', 'b', 'e', 'c', 'd', 'a')
整数转换为字符chr: C
字符chr转换为整数: 67
整数转16进制数: 0xc
整数转8进制数: 0o14
>>> 

 

========================================================

More reading,and english is important.

I'm Hongten

 

大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。
Hongten博客排名在100名以内。粉丝过千。
Hongten出品,必是精品。

E | hongtenzone@foxmail.com  B | http://www.cnblogs.com/hongten

========================================================

 

转载于:https://www.cnblogs.com/hongten/p/hongten_python_convert.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Brother_ql 包是一个 Python 包,用于控制 Brother QL 系列标签打印机。使用 Brother_ql 包,可以轻松地生成标签文件,并将其发送到 Brother QL 打印机进行打印。 下面是使用 Brother_ql 包的简单步骤: 1. 安装 Brother_ql 包: 可以使用 pip 命令来安装 Brother_ql 包,如下所示: ``` pip install brother_ql ``` 2. 创建标签文件: 在 Python 脚本中,可以使用 Brother_ql 包中的 `BrotherQL` 类来创建标签文件。可以设置标签的宽度、高度、边距、字体、颜色等属性,并添加文本和图像等元素。下面是一个简单的示例: ``` from brother_ql import BrotherQL from brother_ql.conversion import convert from PIL import Image printer = BrotherQL('usb://0x04f9:0x2042') img = Image.open('image.png') converted = convert(img, printer, dither=True) printer.write(converted) printer.close() ``` 在上面的示例中,创建了一个 BrotherQL 对象,它代表了一台 Brother QL 打印机。然后,打开一个图像文件,并使用 `convert()` 函数将其转换为打印机可以理解的格式。最后,将转换后的数据写入打印机并关闭连接。 3. 打印标签文件: 创建标签文件后,可以使用 Brother QL 打印机的驱动程序将其发送到打印机进行打印。可以使用 Brother 提供的 P-touch Editor 软件或 Brother QL 打印机驱动程序中的打印 API 进行打印。 总的来说,Brother_ql 包是一个方便的工具,可以帮助 Python 开发人员轻松地控制 Brother QL 系列标签打印机。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值