浅析 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语言 ...
随机推荐
- UVA11137 Ingenuous Cubrency
题意 PDF 分析 考虑dp. 用\(d(i,j)\)表示用不超过i的立方凑成j的方案数. \(d(i,j)=d(i-1,j)+d(i,j-i^3)\) 时间复杂度\(O(IN+T)\) 代码 #in ...
- 异常Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 from http://maven.aliyun.com/nexus/content/groups/public was ...
错误异常:Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 from http://maven ...
- 通过修改注册表建立Windows自定义协议
引言 本文主要介绍注册表的概念与其相关根项的功能,以及浏览器如何通过连接调用自定义协议并与客户端进行数据通信.文中讲及如何通过C#程序.手动修改.安装项目等不同方式对注册表进行修改.其中通过安装项目对 ...
- Linux 制作补丁 打补丁 撤销补丁
1.制作补丁 diff - 逐行比较文件 格式 diff 参数 旧文件/旧文件夹 新文件/新文件夹 -N 将不存在的文件看作是空的 -a 将所有文件都视为文本文件 -u 以合并 ...
- JAVA课程设计(坦克大战)
2019-01-16 坦克大战游戏背景: 1. 需求分析 1.1环境要求 操作系统:Windows 7(SP1)以上 JAVA虚拟机:JDK1.8以上 开发环境:Eclipse(4.5以上) 1.2角 ...
- Apache的下载安装(主要说的 64位)及问题
本文转载自:http://blog.csdn.net/qq_15096707/article/details/47319545 今天重装完win10系统,就重新下载安装 Apache.虽说之前有安装过 ...
- Nginx.PHP配置Smarty
下载http://smarty.net: 解压 -> 将 libs 文件夹重命名 smartyLibs -> 放置在自己服务器的 usr/local/lib/ 中 (/usr/local/ ...
- PAT 甲级 1007 Maximum Subsequence Sum (25)(25 分)(0不是负数,水题)
1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...
- laravel 环境自编译过程
[原创] 看到此文的朋友看完后也许会失望,但我尽最大努力不让搜友们失望,以下是自己操作的笔记用以整理提高 虽然 laravel 官方已给出了安装 laravel 框架所需的环境盒子 使用Vagrant ...
- Cassandra 的启动和初始化
Cassandra常用命令 Cassandra启动过程详解[原创] Cassandra 的入口 CassandraDaemon 作为Cassandra的入口,做了以下几件事: load configu ...