浅析 python中的 print 和 input 的底层区别!!!
近期的项目中 涉及到相关知识 就来总结一下 !
先看源码:
def print(self, *args, sep=' ', end='\n', file=None): # known special case of print
"""
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
""" # 附带翻译哦!
将值打印到溪流或系统中。默认stdout。
可选关键字参数:
文件:类似文件的对象(流);默认为当前的systdout。
sep:在值之间插入字符串,默认空格。
结束:在最后一个值之后附加的字符串,默认换行。
python 源码
print()用sys.stdout.write() 实现
import sys
print('hello')
sys.stdout.write('hello')
print('new')
# 结果:
# hello
# hellonew
sys.stdout.write()结尾没有换行,而print()是自动换行的。另外,write()只接收字符串格式的参数。
print()能接收多个参数输出,write()只能接收一个参数。
input ()
先看源码!
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.
附带 翻译
从标准输入中读取一个字符串 a 。后面的新线被剥掉了。
如果给定的话,提示字符串会被打印到标准输出,而不需要a
在阅读输入之前,跟踪新行。
如果用户点击了EOF(nix:Ctrl-D,Windows:Ctrl-Z+Return),就会产生EOFError。
在nix系统上,如果可用,则使用readline。
input 用 sys.stdin.readline() 实现
import sys a = sys.stdin.readline()
print(a, len(a)) b = input()
print(b, len(b)) # 结果:
# hello
# hello
#
# hello
# hello 5
readline()会把结尾的换行符也算进去。
readline()可以给定整型参数,表示获取从当前位置开始的几位内容。当给定值小于0时,一直获取这一行结束。
import sys a = sys.stdin.readline(3)
print(a, len(a)) # 结果:
# hello
# hel 3
readline()如果给定了整型参数结果又没有把这一行读完,那下一次readline()会从上一次结束的地方继续读,和读文件是一样的。
import sys a = sys.stdin.readline(3)
print(a, len(a)) b = sys.stdin.readline(3)
print(b, len(b)) # 结果
# abcde
# abc 3
# de
#
input()可以接收字符串参数作为输入提示,readline()没有这个功能。
浅析 python中的 print 和 input 的底层区别!!!的更多相关文章
- Python中的print、input函数以及Python中交换两个变量解析
一.Python中的值交换操作 首先明确一点点,Python中的一切都是面向对象的,可以理解为Python的中一切都是对象. 我们知道Java也是面向对象的语言,但是在Java中定义一个值变量如下: ...
- 深入浅析python中的多进程、多线程、协程
深入浅析python中的多进程.多线程.协程 我们都知道计算机是由硬件和软件组成的.硬件中的CPU是计算机的核心,它承担计算机的所有任务. 操作系统是运行在硬件之上的软件,是计算机的管理者,它负责资源 ...
- 【转】浅析Python中的struct模块
[转]浅析Python中的struct模块 最近在学习python网络编程这一块,在写简单的socket通信代码时,遇到了struct这个模块的使用,当时不太清楚这到底有和作用,后来查阅了相关资料大概 ...
- 在python中使用print()时,raw write()返回无效的长度:OSError: raw write() returned invalid length 254 (should have been between 0 and 127)
写出一个不是code的bug,很烦恼,解决了挺长时间,都翻到外文来看,不过还是解决了,只尝试了一种简单可观的方法,希望对大家有用 我正在使用Django与Keras(tensorflow)来训练一个模 ...
- python中dtype,type,astype的区别
python中dtype,type,astype的区别 type() dtype() astype() 函数名称 用法 type 返回参数的数据类型 dtype 返回数组中元素的数据类型 astype ...
- Python中的raw_input()和input()
raw_input()和input()都是python中的内建函数,用于读取控制台用户的输入,但有所区别: [nr@localhost conf]$ python Python 2.7.5 (defa ...
- 浅析python 中__name__ = '__main__' 的作用
引用http://www.jb51.net/article/51892.htm 很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码 ...
- 【转】浅析python 中__name__ = '__main__' 的作用
原文链接:http://www.jb51.net/article/51892.htm 举例说明解释的非常清楚,应该是看到的类似博文里面最简单的一篇: 这篇文章主要介绍了python 中__name__ ...
- 浅析Python中的struct模块
最近在学习python网络编程这一块,在写简单的socket通信代码时,遇到了struct这个模块的使用,当时不太清楚这到底有和作用,后来查阅了相关资料大概了解了,在这里做一下简单的总结. 了解c语言 ...
随机推荐
- elixir grpc 试用
备注: elixir grpc 封装测试 1. 安装 a. 安装 protoc 参考相关文档,比较简单 b. 安装elixir grpc 插件 protoc-gen-elixir 同时配置环 ...
- 记一次socket_create()函数耗时异常记录
背景: 下午开发时突然整个页面耗时增加,空接口每次都需要2-3秒的耗时,一开始以为连开发环境数据库出现问题,最后断开数据库跑,发现还是很慢 最终逐步调试此页面耗时,定位到了socket_create( ...
- redis+php实现微博功能(三)
个人主页显示微博列表(自己及关注人的微博列表) /*获取最新的50微博信息列表,列出自己发布的微博及我关注用户的微博 *1.根据推送的信息获取postid *2.根据postid获取发送的信息 */ ...
- Python nltk English Detection
http://blog.alejandronolla.com/2013/05/15/detecting-text-language-with-python-and-nltk/ >>> ...
- DS05--查找
一.学习总结 1.查找的思维导图 2.查找学习体会 2.1 关联容器和顺序容器 c++中有两种类型的容器:顺序容器和关联容器,顺序容器主要有:vector.list.deque等.其中vector表示 ...
- java Annotation的应用
一.Annotation 示例 Override Annotation @Override public void onCreate(Bundle savedInstanceState); 二.Ann ...
- Jquery学习小计
实时监听输入框值变化 首先创建Jquery.fn扩展 jQuery.fn.extend({ inputChange: function(callback){ if($.support.leadingW ...
- charles 设置弱网测试
Charles简介:Charles支持抓去http.https协议的请求,不支持socket. 然后charles会自动配置IE浏览器和工具的代理设置,所以说打开工具直接就已经是抓包状态了. 这里打开 ...
- Vmware虚拟机linux上网问题
1.虚拟机linux上网问题 1.1 VMware中虚拟机网络的三种设置 第一种:桥接(bridged) 第二种:NAT 第三种:Host only . 这种模式下仅主机可以上网,虚拟机不能上网. 1 ...
- Histogram
folly/Histogram.h Classes Histogram Histogram.h defines a simple histogram class, templated on the t ...