python编程开发之类型转换convert实例分析
作者:Hongten 时间:2023-03-24 05:48:06
本文实例讲述了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:
#类型转换
#convert
#convert to int
print('int()默认情况下为:', int())
print('str字符型转换为int:', int('010'))
print('float浮点型转换为int:', int(234.23))
#十进制数10,对应的2进制,8进制,10进制,16进制分别是:1010,12,10,0xa
print('int(\'0xa\', 16) = ', int('0xa', 16))
print('int(\'10\', 10) = ', int('10', 10))
print('int(\'12\', 8) = ', int('12', 8))
print('int(\'1010\', 2) = ', int('1010', 2))
#convert to long
print('int浮点型转换为int:', int(23))
#convert to float
print('float()默认情况下为:', float())
print('str字符型转换为float:', float('123.01'))
print('int浮点型转换为float:', float(32))
#covert to complex
print('创建一个复数(实部+虚部):', complex(12, 43))
print('创建一个复数(实部+虚部):', complex(12))
#convert to str
print('str()默认情况下为:', str())
print('float字符型转换为str:', str(232.33))
print('int浮点型转换为str:', str(32))
lists = ['a', 'b', 'e', 'c', 'd', 'a']
print('列表list转换为str:', ''.join(lists))
#covert to list
strs = 'hongten'
print('序列strs转换为list:', list(strs))
#covert to tuple
print('列表list转换为tuple:', tuple(lists))
#字符和整数之间的转换
#char coverted to int
print('整数转换为字符chr:', chr(67))
print('字符chr转换为整数:', ord('C'))
print('整数转16进制数:', hex(12))
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
>>>
希望本文所述对大家Python程序设计有所帮助。
标签:python,类型转换
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于Python实现图片一键切割九宫格的工具
2022-07-05 00:22:31
pytorch 如何把图像数据集进行划分成train,test和val
2023-12-26 15:28:10
![](https://img.aspxhome.com/file/2023/4/72124_0s.png)
Sqlserver 2005附加数据库时出错提示操作系统错误5(拒绝访问)错误5120的解决办法
2024-01-27 10:14:51
![](https://img.aspxhome.com/file/2023/6/104126_0s.jpg)
Python2与Python3的区别实例分析
2021-01-07 11:47:17
Golang自旋锁的相关介绍
2024-05-09 14:58:22
mysql查询优化之100万条数据的一张表优化方案
2024-01-13 20:10:04
Django中使用Whoosh进行全文检索的方法
2023-03-20 05:35:43
浅谈python写入大量文件的问题
2021-12-07 00:44:00
python读取Kafka实例
2023-10-22 17:22:58
![](https://img.aspxhome.com/file/2023/1/65481_0s.jpg)
跟老齐学Python之总结参数的传递
2021-12-16 18:17:24
numpy实现神经网络反向传播算法的步骤
2021-02-11 10:54:34
![](https://img.aspxhome.com/file/2023/5/77915_0s.jpg)
Python实现计算图像RGB均值方式
2024-01-01 20:50:31
![](https://img.aspxhome.com/file/2023/3/99513_0s.jpg)
在Python中使用Mako模版库的简单教程
2021-11-08 12:33:45
MySQL无服务及服务无法启动的终极解决方案分享
2024-01-26 18:04:39
![](https://img.aspxhome.com/file/2023/3/106583_0s.png)
ASP连接MSSQL的错误: 拒绝访问
2008-11-23 20:40:00
如何将sql执行的错误消息记录到本地文件中实现过程
2024-01-22 06:50:37
![](https://img.aspxhome.com/file/2023/0/122400_0s.jpg)
ASP平台如何打造金牌英文网站
2008-04-10 08:47:00
python中的内置函数max()和min()及mas()函数的高级用法
2023-01-21 18:45:43
![](https://img.aspxhome.com/file/2023/6/76096_0s.jpg)
Python多进程模式实现多核CPU并行计算
2022-12-01 21:26:20
golang 比较浮点数的大小方式
2024-04-28 10:48:50