Python自动化--1.Python环境安装-linux Python自动化--2.Python变量 Python自动化--3.Python数据类型 Python自动化--4.python类型转换 Python自动化--5. if判断语句
Python自动化--1.Python环境安装-linux
Python自动化--2.Python变量
Python自动化--3.Python数据类型
Python自动化--4.python类型转换
Python自动化--5. if判断语句
Python自动化--6. 写一个python程序
Python自动化--7. 函数的定义和调用
4.1 转换为int
In [22]: int('10')Out[22]: 10
In [23]: int('-10')
Out[23]: -10
In [24]: int (1.1)
Out[24]: 1
In [26]: int (1.5)
Out[26]: 1
4.2 转换为float
In [27]: float(1)Out[27]: 1.0
In [28]: float(-1)
Out[28]: -1.0
In [29]: float('1.1')
Out[29]: 1.1
4.3 转换为str
In [30]: str(190)Out[30]: '190'
In [31]: str(-9089)
Out[31]: '-9089'
In [32]: str(-000)
Out[32]: '0'
In [33]: str(-11.11)
Out[33]: '-11.11'
In [34]: str(b'hello', encoding='utf-8')
Out[34]: 'hello'
#二进制转换字符串时,需指定字符编码
4.4 转换为二进制
In [35]: bytes('小冬', encoding=('utf-8'))Out[35]: b'\xe5\xb0\x8f\xe5\x86\xac'
#字符串转换二进制时,需指定字符编码
In [36]: b = bytes('小冬', encoding=('utf-8'))
In [37]: b
Out[37]: b'\xe5\xb0\x8f\xe5\x86\xac'
In [38]: str(b, encoding='utf-8')
Out[38]: '小冬'In [41]: s = str(b, encoding='utf-8')
In [42]: s
Out[42]: '小冬'
In [43]: type(s)
Out[43]: str