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