菜鸟教程 -- 学的不仅是技术,更是梦想!

Python2.x 和 Python3.x 中 raw_input( ) 和 input( ) 区别

分类 编程技术

1、在 Python2.x 中 raw_input( ) 和 input( ),两个函数都存在,其中区别为:

  • raw_input( ) 将所有输入作为字符串看待,返回字符串类型。
  • input( ) 只能接收"数字"的输入,在对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float )。

2、在 Python3.x 中 raw_input( )input( ) 进行了整合,去除了 raw_input( ),仅保留了 input( ) 函数,其接收任意任性输入,将所有输入默认为字符串处理,并返回字符串类型。

例如:

Python 2.3.4 (#1, Feb 2 2005, 11:44:13) 
[GCC 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> user=raw_input("please input:") 
please input:wei # raw_input 输入 字符串 成功
>>> user
'wei'
>>> user=input("please input:") 
please input:123 # input 输入 数字 成功(返回的是数字)
>>> user
123
>>> user=raw_input("please input:")
please input:111       # raw_input 输入 数字 成功(返回的还是当成字符串)
>>> user
'111'
>>> user=input("please input:")
please input:wei # input 输入字符串 失败
Traceback (most recent call last):
 File "<stdin>", line 1, in ?
 File "<string>", line 0, in ?
NameError: name 'wei' is not defined

在 Python 3.2.3 中 input 和 raw_input 整合了,没有了 raw_input:

Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> user=raw_input("please input:") #没有了raw_input
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
NameError: name 'raw_input' is not defined
>>> user=input("please input:")
please input:wei
>>> user
'wei'
>>> user=input("please input:") #input的输出结果都是作为字符串
please input:123
>>> user
'123'

原文地址:https://blog.csdn.net/suibianshen2012/article/details/51378948

1 篇笔记 写笔记

  1. #0

    qykqlr

    qyk***@163.com

    22

    Python2.x 中的 input() 并不是只能接收数字。

    Python2.x 中 input() 相等于 eval(raw_input(prompt)) ,用来获取控制台的输入。raw_input() 将所有输入作为字符串看待,返回字符串类型。而 input() 在对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float )。

    注意:input() 和 raw_input() 这两个函数均能接收 字符串 ,但 raw_input() 直接读取控制台的输入(任何类型的输入它都可以接收)。而对于 input() ,它希望能够读取一个合法的 python 表达式,即你输入字符串的时候必须使用引号将它括起来,否则它会引发一个 SyntaxError 。

    除非对 input() 有特别需要,否则一般情况下我们都是推荐使用 raw_input() 来与用户交互。

    注意:Python3 里 input() 默认接收到的是 str 类型。

    qykqlr

    qyk***@163.com

    8年前 (2019年01月29日)

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址
ADO 教程 AI Agent 教程 AI 入门教程 Ajax 教程 Android 教程 Angular2 教程 AngularJS 教程 AppML 教程 ASP 教程 ASP.NET 教程 Bootstrap 教程 Bootstrap4 教程 Bootstrap5 教程 C 教程 C# 教程 C++ 教程 Chart.js 教程 Claude Code 教程 CMake 教程 Codex 教程 CSS 参考手册 CSS 教程 CSS3 教程 Cursor 教程 Dart 教程 Dash 教程 Django 教程 Docker 教程 DTD 教程 ECharts 教程 Eclipse 教程 Electron 教程 FastAPI 教程 Firebug 教程 Flask 教程 Flutter 教程 Font Awesome 图标 Foundation 教程 Git 教程 Go 语言教程 Google 地图 API 教程 Hermes Agent Highcharts 教程 HTML DOM 教程 HTML 参考手册 HTML 字符集 HTML 教程 HTTP 教程 ionic 教程 iOS 教程 Java 教程 JavaScript 参考手册 Javascript 教程 jQuery EasyUI 教程 jQuery Mobile 教程 jQuery UI 教程 jQuery 教程 JSON 教程 JSP 教程 Julia 教程 Jupyter Notebook 教程 Kotlin 教程 LangChain 教程 LaTeX 教程 Linux 教程 Lua 教程 Markdown 教程 Matplotlib 教程 Maven 教程 Memcached 教程 MongoDB 教程 MySQL 教程 Next.js 教程 NLP 教程 Node.js 教程 NumPy 教程 Obsidian 教程 Ollama 教程 OpenCode 教程 OpenCV 教程 Pandas 教程 Perl 教程 PHP 教程 Pillow 教程 Playwright 教程 PostgreSQL 教程 PowerShell Pycharm 教程 Python 3 教程 Python 基础教程 Python 设计模式 Python 量化交易 PyTorch 教程 R 教程 RDF 教程 React 教程 Redis 教程 RESTful API RSS 教程 Ruby 教程 Rust 教程 Sass 教程 Scala 教程 SciPy 教程 Selenium 教程 Servlet 教程 Skills 教程 Sklearn 教程 SOAP 教程 SQL 教程 SQLite 教程 SVG 教程 SVN 教程 Swagger 教程 Swift 教程 Tailwind CSS 教程 TCP/IP 教程 TensorFlow 教程 TypeScript 教程 uni-app 教程 VBScript 教程 Vibe Coding 教程 VSCode 教程 Vue.js 教程 Vue3 教程 W3C 教程 Web Service 教程 WSDL 教程 XLink 教程 XML DOM 教程 XML Schema 教程 XML 教程 XPath 教程 XQuery 教程 XSLFO 教程 XSLT 教程 Zig 教程 数据结构 机器学习 正则表达式 汇编语言 测验 浏览器 网站品质 网站建设指南 网站服务器教程 网络协议 设计模式

AltStyle によって変換されたページ (->オリジナル) /