python里的input
2024-10-11 17:50:29
python2和python3的input是不同的
python3的input
对于python3,只有input,官方文档里是这样描述的
def input(*args, **kwargs): # real signature unknown
"""
Read a string from standard input. The trailing newline is stripped.
The prompt string, if given, is printed to standard output without a
trailing newline before reading input.
If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
On *nix systems, readline is used if available.
"""
pass
意思就是:读取一个字符串并输入,舍弃结尾的换行符
```python
a = input()
print(a, type(a))
b = input()
print(b, type(b))
<br>
控制台输出结果
```python
hello
hello <class 'str'>
123
123 <class 'str'>
python2的input
python2有input和raw_input两种输入
input
a = input()
print(a, type(a))
b = input()
print(b, type(b))
控制台输出结果
123
(123, <type 'int'>)
hello
Traceback (most recent call last):
File "D:/input_test/test.py", line 11, in <module>
b = input()
File "<string>", line 1, in <module>
NameError: name 'hello' is not defined
报错了!这是因为input是获取原始的输入内容,也就是说,输入什么,就会得到什么
官方文档是这样描述的
def input(prompt=None): # real signature unknown; restored from __doc__
"""
input([prompt]) -> value
Equivalent to eval(raw_input(prompt)).
"""
pass
如果要输入字符串,需要手动加引号
a = raw_input()
print(a, type(a))
b = raw_input()
print(b, type(b))
# 控制台输出结果
123
(123, <type 'int'>)
'hello'
('hello', <type 'str'>)
### raw_input
raw_input与python3里面的input一样,输入的内容都会转化成字符串
a = raw_input()
print(a, type(a))
b = raw_input()
print(b, type(b))
控制台输出结果
123
('123', <type 'str'>)
hello
('hello', <type 'str'>)
## 小结
- python3只有input,输入的数据都会转化成字符串
- python2有input和raw_input,input读取原始的数据类型,输入什么就得到什么;raw_input获取到的都是字符串类型
补充:关于input的底层实现,参考博客 python中print和input的底层实现
最新文章
- WinPhone学习笔记(五)——LongListSelector
- ubuntu下配置和使用ssh
- Java API 快速速查宝典
- Mongodb Management Studio
- django HTTP请求(Request)和回应(Response)对象
- Cogs 1672. [SPOJ375 QTREE]难存的情缘 LCT,树链剖分,填坑计划
- Spring下@ResponseBody响应中文内容乱码问题
- 在PHP中开启CURL扩展,使其支持curl()函数
- 如何实现android蓝牙开发 自动配对连接,并不弹出提示框
- 多平台Client TCP通讯组件
- DirectX--yuv420p上实现的字符叠加
- 转载:如何搭建turn server 在centos7上。
- cadence 17.2 安装破解
- 配置MQTT服务器
- 疯狂安装oracle 12c,此版本没有scott这个用户
- Mina的ssl加密
- KVOController原理解析
- 我的Mac使用笔记
- 0605-Zuul构建API Gateway-使用Sidecar支持异构平台的微服务
- java中把指数形式的数字转为正常形式显示
热门文章
- open-falcon详解
- 【POJ1704】Georgia and Bob(博弈论)
- [hgoi#2019/3/21]NOIP&;NOI赛后总结
- [bzoj2288][pojChallenge]生日礼物【贪心+堆+链表】
- 深挖JDK动态代理(二):JDK动态生成后的字节码分析
- Linux登录那点事
- 【转】服务化框架技术选型与京东JSF解密
- CF集萃1
- [luogu3391][文艺平衡树]
- Dreamweaver - <;!DOCTYPE html>;