Python基礎教程:input()輸入與數據類型轉換

input就是個萬能輸入,不過input輸入的元素都是以str形式保存的,若是要他做爲一個整數的話,就須要進行數據類型轉換。python

input的使用code

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)))

數據類型轉換input

>>> 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
相關文章
相關標籤/搜索