網站首頁 編程語言 正文
input()
input() 是 Python 的內置函數,用于從控制臺讀取用戶輸入的內容。input() 函數總是以字符串的形式來處理用戶輸入的內容,所以用戶輸入的內容可以包含任何字符。
input() 函數的用法為:
str = input(tipmsg)
說明:
- str 表示一個字符串類型的變量,input 會將讀取到的字符串放入 str 中。
- tipmsg 表示提示信息,它會顯示在控制臺上,告訴用戶應該輸入什么樣的內容;如果不寫 tipmsg,就不會有任何提示信息。
【實例】input() 函數的簡單使用:
a =input("Enter a number: ")
b =input("Enter another number: ")
print("aType: ",type(a))
print("bType: ",type(b))
result = a + b
print("resultValue: ", result)
print("resultType: ",type(result))
運行結果示例:
Enter a number: 100 ↙
Enter another number: 45 ↙
aType: <class 'str'>
bType: <class 'str'>
resultValue: 10045
resultType: <class 'str'>
↙表示按下回車鍵,按下回車鍵后 input() 讀取就結束了。
本例中我們輸入了兩個整數,希望計算出它們的和,但是事與愿違,Python 只是它們當成了字符串,+起到了拼接字符串的作用,而不是求和的作用。
我們可以使用 Python 內置函數將字符串轉換成想要的類型,比如:
- int(string) 將字符串轉換成 int 類型;
- float(string) 將字符串轉換成 float 類型;
- bool(string) 將字符串轉換成 bool 類型。
修改上面的代碼,將用戶輸入的內容轉換成數字:
a =input("Enter a number: ")
b =input("Enter another number: ")
a =float(a)
b =int(b)
print("aType: ",type(a))
print("bType: ",type(b))
result = a + b
print("resultValue: ", result)
print("resultType: ",type(result))
運行結果:
Enter a number: 12.5 ↙
Enter another number: 64 ↙
aType: <class 'float'>
bType: <class 'int'>
resultValue: 76.5
resultType: <class 'float'>
關于 Python 2.x
上面講解的是 Python 3.x 中 input() 的用法,但是在較老的 Python 2.x 中情況就不一樣了。Python 2.x 共提供了兩個輸入函數,分別是 input() 和 raw_input():
- Python 2.x raw_input() 和 Python 3.x input() 效果是一樣的,都只能以字符串的形式讀取用戶輸入的內容。
- Python 2.x input() 看起來有點奇怪,它要求用戶輸入的內容必須符合 Python 的語法,稍有疏忽就會出錯,通常來說只能是整數、小數、復數、字符串等。
比較強迫的是,Python 2.x input() 要求用戶在輸入字符串時必須使用引號包圍,這有違 Python 簡單易用的原則,所以 Python 3.x 取消了這種輸入方式。
修改本節第一段代碼,去掉 print 后面的括號:
a =input("Enter a number: ")
b =input("Enter another number: ")
print"aType: ",type(a)
print"bType: ",type(b)
result = a + b
print"resultValue: ", result
print"resultType: ",type(result)
在 Python 2.x 下運行該代碼:
Enter a number: 45 ↙
Enter another number: 100 ↙
aType: <type 'int'>
bType: <type 'int'>
resultValue: 145
resultType: <type 'int'>
原文鏈接:https://blog.csdn.net/m0_74309242/article/details/128636935
相關推薦
- 2022-06-26 python數據處理之Pandas類型轉換的實現_python
- 2022-08-31 React?SSR?中的限流案例詳解_React
- 2022-12-07 C++?兩個vector對象拼接方式_C 語言
- 2022-05-12 tp5使用阿里云oss存儲圖片
- 2023-04-18 利用Python操作MongoDB數據庫的詳細指南_python
- 2022-07-24 C語言超詳細i講解雙向鏈表_C 語言
- 2022-05-15 Qt Linux獲取bios ID作為唯一標識
- 2023-12-22 CSS濾鏡(filter)
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支