近期的项目中 涉及到相关知识 就来总结一下 !

先看源码:

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 的底层区别!!!的更多相关文章

  1. Python中的print、input函数以及Python中交换两个变量解析

    一.Python中的值交换操作 首先明确一点点,Python中的一切都是面向对象的,可以理解为Python的中一切都是对象. 我们知道Java也是面向对象的语言,但是在Java中定义一个值变量如下: ...

  2. 深入浅析python中的多进程、多线程、协程

    深入浅析python中的多进程.多线程.协程 我们都知道计算机是由硬件和软件组成的.硬件中的CPU是计算机的核心,它承担计算机的所有任务. 操作系统是运行在硬件之上的软件,是计算机的管理者,它负责资源 ...

  3. 【转】浅析Python中的struct模块

    [转]浅析Python中的struct模块 最近在学习python网络编程这一块,在写简单的socket通信代码时,遇到了struct这个模块的使用,当时不太清楚这到底有和作用,后来查阅了相关资料大概 ...

  4. 在python中使用print()时,raw write()返回无效的长度:OSError: raw write() returned invalid length 254 (should have been between 0 and 127)

    写出一个不是code的bug,很烦恼,解决了挺长时间,都翻到外文来看,不过还是解决了,只尝试了一种简单可观的方法,希望对大家有用 我正在使用Django与Keras(tensorflow)来训练一个模 ...

  5. python中dtype,type,astype的区别

    python中dtype,type,astype的区别 type() dtype() astype() 函数名称 用法 type 返回参数的数据类型 dtype 返回数组中元素的数据类型 astype ...

  6. Python中的raw_input()和input()

    raw_input()和input()都是python中的内建函数,用于读取控制台用户的输入,但有所区别: [nr@localhost conf]$ python Python 2.7.5 (defa ...

  7. 浅析python 中__name__ = '__main__' 的作用

    引用http://www.jb51.net/article/51892.htm 很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码 ...

  8. 【转】浅析python 中__name__ = '__main__' 的作用

    原文链接:http://www.jb51.net/article/51892.htm 举例说明解释的非常清楚,应该是看到的类似博文里面最简单的一篇: 这篇文章主要介绍了python 中__name__ ...

  9. 浅析Python中的struct模块

    最近在学习python网络编程这一块,在写简单的socket通信代码时,遇到了struct这个模块的使用,当时不太清楚这到底有和作用,后来查阅了相关资料大概了解了,在这里做一下简单的总结. 了解c语言 ...

随机推荐

  1. Sprint第一个冲刺(第二天)

    一.Sprint介绍 今天我们完成了欢迎界面及美化的任务,即完成了此次整个Sprint的百分之七,登录界面及美化.注册界面(管理员).注册界面(餐厅员工)和注册界面(消费者),这几个任务也在进行时.这 ...

  2. [MEF]第03篇 MEF延迟加载导出部件及元数据

    一.演示概述此演示介绍了MEF的延迟加载及元数据相关的内容.在实际的设计开发中,存在着某些对象是不需要在系统运行或者附属对象初始化的时候进行实例化的,只需要在使用到它的时候才会进行实例化,这种方式就可 ...

  3. ArangoDB Foxx service 使用

    备注:   项目使用的是github https://github.com/arangodb-foxx/demo-hello-foxx 1. git clone git clone https://g ...

  4. Automating CSS Regression Testing

    The following is a guest post by Garris Shipon . We've touched on the four types of CSS testing here ...

  5. php基础语法(控制语句、数组、函数)

    流程控制 if -else if -else语句: switch语句: while循环: do while循环 for循环: 控制脚本执行进度 die(“输出内容”) exit是die的同义词. sl ...

  6. http接口测试工具

    2016-08-28 19:24:55 以全国天气预报为例  https://www.juhe.cn/docs/api/id/39/aid/132 (一)火狐的HttpRequester 在URL中填 ...

  7. mac版sublime 无法下载插件(Vue 代码无高亮问题)

    1.官方下载sublime(http://www.sublimetext.com/3) 然后需要两步(1)上传插件主包Package Control(2)更改channels的配置信息(原来是国外的需 ...

  8. GOF23设计模式之访问者模式(visitor)

    一.访问者模式概述 (1)模式动机   对于存储在一个集合中的对象,他们可能具有不同的类型(即使有一个公共的接口),对于该集合中的对象,可以接受一类称为访问者的对象来访问,不同的访问者其访问方式也有所 ...

  9. java内存占用问题(一)

    Nocturne 2012-12-24 java数组内存占用问题. 30 Contact[] ca = new Contact[10];   while(x<10){     ca[x]=new ...

  10. linux环境下搭建MySQL

    linux下搭建mysql的方式很多,网上也详解了很多种搭建方式,有直接yum的.有rpm的..总之,“坑”是层出不穷,有相关文件依赖性.权限.GPG keys等等. 本人也在今天搭建了一下.是出“坑 ...