python學習筆記一: 《python3 input()函數》


一、在學習之前需要先了解:

1、Python3.x 中 input() 函數接受一個標准輸入數據,返回為 string 類型,即把任何輸入看作str

2、input可以用作文本輸入,如用戶名,密碼框的值輸入

3、需要注意:在 Python3.x 中 raw_input() 和 input() 進行了整合,去除了 raw_input( ),僅保留了input( )函數,其接收任意任性輸入,將所有輸入默認為字符串處理,並返回字符串類型。

 

二、實踐操作:

1、我們在IDLE中進行快捷的驗證:

>>> a=input("input:")
input:123
>>> type(a)
<class 'str'>

---說明input作為一個文本輸入,它返回的都是string類型

2、為了驗證raw_input是否在python3.x中被整合我們驗證:

>>> b=raw_input("input")
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
b=raw_input("input")
NameError: name 'raw_input' is not defined

--驗證如上報錯了,已經發現未定義raw_input,至此 python3.x驗證只使用input

 

3、由於input()函數只能返回str,那么我們想要int 或者其他類型應該如何處理

需要用到:int(),str(),float()等修飾符

實踐:

>>> a=input("輸入整數:")
輸入整數:123
>>> b=input("輸入浮點數:")
輸入浮點數:1.34
>>> c=input("輸入字符串:")
輸入字符串:hello

結果:

>>> type(a)
<class 'str'>
>>> type(b)
<class 'str'>
>>> type(c)
<class 'str'>

再執行:

>>> int(a)
123
>>> float(b)
1.34
>>> str(c)
'hello'
>>> print(a,b,c)
123 1.34 hello

 

可以看到以上通過 int().float()可以轉化為我們想要的字符串或者整型,浮點型等

好,第一個學習結束!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM