常用的数据类型转换
- int(x) 将x转换为一个整数
- float(x) 将x转换为一个浮点数
- str(x) 将x转换为一个字符串
>>>password = input("密码:")
密码:123456
>>>
>>>password
'123456'
>>>type(password)
<class 'str'>
>>>
>>>a = int(password)
>>>a
123456
>>>type(a)
<class 'int'>
>>>
>>>b = int("123456")
>>>b
123456
>>>type(b)
<class 'int'>
>>>
>>>c = int("3.14")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '3.14'
>>>c = float("3.14")
>>>c
3.14
>>>type(c)
<class 'float'>
>>>
网友评论