Python基础教程:input()输入与数据类型转换


input就是个万能输入,不过input输入的元素都是以str形式保存的,如果要他作为一个整数的话,就需要进行数据类型转换。

input的使用

name=input('please input your name :\n')
print(name)
please input your name : 
152 365 
152 365 #这是个字符串

name=input('please input your name :\n').split()
print(name)
please input your name :
152 365
['152', '365'] #这是个list

示例

#输入多个整数,输出他们的十六进制
num=input('please input nums').split()
for i in num:
    print(i,':',hex(int(i)))

数据类型转换

>>> int('123')
123
>>> float('12.34')
12.34
>>> int(12.34)
12
>>> float(12)
12.0
>>> str(1.23)
'1.23'
>>> str(100)
'100'
>>> bool(1)
True
>>> bool('')
False


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM