python里的input
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的更多相关文章
- Python里的类和对象简介
---恢复内容开始--- Python里的类 对象=属性+方法: 对象的属性主要是指主要的特征和参量,而方法主要是指函数: 类是一个具有一定特征和方法的集合,而对象是类的一个:类和对象的关系就如同模 ...
- Python中的input你真会吗?
前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:一米阳光里的晴天娃娃 python中的input()方法是在控制台可 ...
- python里的文件I/O
Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你 ...
- python 中的input
渣渣之路. 一. 在python编程初学者指南中的第六章.使用参数和返回值的例子中: # -*- coding: utf-8 -*- def display(message): print messa ...
- 为什么在Python里推荐使用多进程而不是多线程
转载 http://bbs.51cto.com/thread-1349105-1.html 最近在看Python的多线程,经常我们会听到老手说:"Python下多线程是鸡肋,推荐使用多进程 ...
- 为什么在Python里推荐使用多进程而不是多线程?
最近在看Python的多线程,经常我们会听到老手说:“Python下多线程是鸡肋,推荐使用多进程!”,但是为什么这么说呢? 要知其然,更要知其所以然.所以有了下面的深入研究: 首先强调背景: 1. ...
- Python里format()方法基本使用
'''第一种:自然连接''' #format 连接字符串 str = '{}使用的python是{}版本'.format('我','3.6.5') print(str) #打印结果:我使用的pytho ...
- Python里的单下划线,双下划线,以及前后都带下划线的意义
Python里的单下划线,双下划线,以及前后都带下划线的意义: 单下划线如:_name 意思是:不能通过from modules import * 导入,如需导入需要:from modules imp ...
- 为什么在Python里推荐使用多进程而不是多线程?(为什么python多线程无法增加CPU使用率?)
最近在看Python的多线程,经常我们会听到老手说:“Python下多线程是鸡肋,推荐使用多进程!”,但是为什么这么说呢? 要知其然,更要知其所以然.所以有了下面的深入研究: 首先强调背景: ...
随机推荐
- 【BZOJ5305】[HAOI2018]苹果树(组合计数)
[BZOJ5305][HAOI2018]苹果树(组合计数) 题面 BZOJ 洛谷 题解 考虑对于每条边计算贡献.每条边的贡献是\(size*(n-size)\). 对于某个点\(u\),如果它有一棵大 ...
- canvas路径剪切和判断是否在路径内
1.剪切路径 clip() var ctx=mycanvas.getContext('2d'); ctx.beginPath(); // 建一个矩形路径 ctx.moveTo(20,10) ctx.l ...
- ZOJ 4062 Plants vs. Zombies(二分答案)
题目链接:Plants vs. Zombies 题意:从1到n每个位置一棵植物,植物每浇水一次,增加ai高度.人的初始位置为0,人每次能往左或往右走一步,走到哪个位置就浇水一次.求m步走完后最低高度的 ...
- uvaLive7303 Aquarium (kruskal)
题意:给R*C的房间,每个房间被左上-右下或右上-左下的墙分割为两个小房间,将分割移除有一定花费,问使所有小房间联通需要的最小花费 把每个房间分成左右(上下?)两个点,判一判,本来就联通的加零边,一个 ...
- ftp sun jdk自带
package com.italktv.colnv.stat.util; import java.io.File; import java.io.FileInputStream; import jav ...
- js click 与 onclick 事件绑定,触发与解绑
click 与 onclick 1.onclick 事件会在对象被点击时发生. <input id="btn1" type="button" onclic ...
- 洛谷P3338 力
题意: 解: 介绍两种方法. 首先可以把那个最后除的qi拆掉. ①分前后两部分处理. 前一部分可以看做是个卷积.下面的平方不拆开,直接看成gi-j即可. 后一部分按照套路,把循环变量改成从0开始,反转 ...
- bzoj1791[IOI2008]Island岛屿(基环树+DP)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1791 题目大意:给你一棵n条边的基环树森林,要你求出所有基环树/树的直径之和.n< ...
- 团体程序设计天梯赛(CCCC) L3014 周游世界 BFS证明
团体程序设计天梯赛代码.体现代码技巧,比赛技巧. https://github.com/congmingyige/cccc_code
- 最短路 次短路 k短路(k很小)
最短路 luogu 3371 https://www.luogu.org/problemnew/show/P3371 #include <cstdio> #include <cstd ...