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的底层实现

python里的input的更多相关文章

  1. Python里的类和对象简介

    ---恢复内容开始--- Python里的类  对象=属性+方法: 对象的属性主要是指主要的特征和参量,而方法主要是指函数: 类是一个具有一定特征和方法的集合,而对象是类的一个:类和对象的关系就如同模 ...

  2. Python中的input你真会吗?

    前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:一米阳光里的晴天娃娃   python中的input()方法是在控制台可 ...

  3. python里的文件I/O

    Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你 ...

  4. python 中的input

    渣渣之路. 一. 在python编程初学者指南中的第六章.使用参数和返回值的例子中: # -*- coding: utf-8 -*- def display(message): print messa ...

  5. 为什么在Python里推荐使用多进程而不是多线程

    转载  http://bbs.51cto.com/thread-1349105-1.html 最近在看Python的多线程,经常我们会听到老手说:"Python下多线程是鸡肋,推荐使用多进程 ...

  6. 为什么在Python里推荐使用多进程而不是多线程?

    最近在看Python的多线程,经常我们会听到老手说:“Python下多线程是鸡肋,推荐使用多进程!”,但是为什么这么说呢?   要知其然,更要知其所以然.所以有了下面的深入研究: 首先强调背景: 1. ...

  7. Python里format()方法基本使用

    '''第一种:自然连接''' #format 连接字符串 str = '{}使用的python是{}版本'.format('我','3.6.5') print(str) #打印结果:我使用的pytho ...

  8. Python里的单下划线,双下划线,以及前后都带下划线的意义

    Python里的单下划线,双下划线,以及前后都带下划线的意义: 单下划线如:_name 意思是:不能通过from modules import * 导入,如需导入需要:from modules imp ...

  9. 为什么在Python里推荐使用多进程而不是多线程?(为什么python多线程无法增加CPU使用率?)

    最近在看Python的多线程,经常我们会听到老手说:“Python下多线程是鸡肋,推荐使用多进程!”,但是为什么这么说呢? 要知其然,更要知其所以然.所以有了下面的深入研究: 首先强调背景:     ...

随机推荐

  1. 【HDU - 4341】Gold miner(分组背包)

    BUPT2017 wintertraining(15) #8B 题意 给出每个黄金的坐标.价值及耗时,同一方向的黄金只能依次取,求T时间内收获的最大值. 题解 同一方向,物品前缀和构成的组合,相当于是 ...

  2. 【CF1139D】Steps to One(动态规划)

    [CF1139D]Steps to One(动态规划) 题面 CF 你有一个数组,每次随机加入一个\([1,n]\)的数,当所有数\(gcd\)为\(1\)时停止,求数组长度的期望. 题解 设\(f[ ...

  3. [luogu3648][bzoj3675][APIO2014]序列分割【动态规划+斜率优化】

    题目大意 让你把一个数列分成k+1个部分,使分成乘积分成各个段乘积和最大. 分析 首先肯定是无法开下n \(\times\) n的数组,那么来一个小技巧:因为我们知道k的状态肯定是从k-1的状态转移过 ...

  4. js click 与 onclick 事件绑定,触发与解绑

    click 与 onclick 1.onclick 事件会在对象被点击时发生. <input id="btn1" type="button" onclic ...

  5. A1042. Shuffling Machine

    Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...

  6. java面试——问题回溯

    背景:用来记录面试过程中遇到的问题,在这里进行记录,下次不要犯同样的错误. 迪普科技 Linux服务器下的top命令 #动态更新的虚拟文件实际上是许多其他内存相关工具(如:free / ps / to ...

  7. JavaScript(JS)基本语法(一)

    https://www.cnblogs.com/haiyan123/p/7577598.html 一.JavaScript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入 ...

  8. java基本数据类型转换溢出问题

    java的基本数据类型有(int.byte.double.float.char.boolean.long.short):这里介绍整型数据 示例1: public class H_Z01 { publi ...

  9. HTTP请求头和响应头的格式

    请求头: 请求头肯定带着客户端信息,比如host主机名,User-Agent用户代理信息,Cookie等等  响应头: 响应头带有服务端信息:Server服务器信息,Last-Modified最后修改 ...

  10. position:fixed固定定位的用法

    一.position:fixed:固定定位 1.实现某个元素在可视窗口的居中位置显示 1)给自身设置宽高: 2)给自身加position:fixed: 3)用margin向左移动自身宽度的一半,向上移 ...