python中in的底层实现_python中print和input的底层实现
print的底层通过sys.stdout.write() 实现
import sys
print('hello')
print('world')
print(520)
sys.stdout.write('hello')
sys.stdout.write('world')
# sys.stdout.write(520) # TypeError: write() argument must be str, not int
控制台输出
```python
hello
world
520
helloworld
## 小结
sys.stdout.write()和print都是向屏幕输出内容,区别在于:
- sys.stdout.write()没有自动换行,print有
- sys.stdout.write()只能写入字符串,print可以写入任意数据类型
## input
Python3中的input()使用`sys.stdin.readline()`实现
```python
import sys
a = sys.stdin.readline()
print(a, len(a))
b = input()
print(b, len(b))
控制台输出结果
```python
hello
hello
6
hello
hello 5
为什么两个长度不一样呢?这是因为sys.stdin.readline()把结尾的换行符也算进去了,6和hello不在一行也可以证明这一点
sys.stdin.readline()还可以传入参数,指定读取前几个字符
```python
c = sys.stdin.readline(2)
print(c, len(c))
控制台输出
hello
he 2
当传入的参数大于输入的字符的长度时,输出所有字符
```python
c = sys.stdin.readline(10)
print(c, len(c))
控制台输出结果
```python
hello
hello
6
当传入的参数为负数时,表示获取整行
```python
d = sys.stdin.readline(-3)
print(d, len(d))
控制台输出结果
```python
helloworld
helloworld
11
当一次没有读取完时,下一次读取将会从上次的结束位置开始,这一点与文件类似
```python
c = sys.stdin.readline(6)
print(c, len(c))
d = sys.stdin.readline(4)
print(d, len(d))
控制台输出结果
```python
helloworld
hellow 6
orld 4
sys.stdin.readline()不能像input一样传入字符串作为提示信息
```python
username = input("username:")
password = sys.stdin.readline("password:")
控制台输出结果
```python
username:robin
Traceback (most recent call last):
File "D:/input_print.py", line 50, in
password = sys.stdin.readline("password:")
TypeError: 'str' object cannot be interpreted as an integer
## 小结
sys.stdin.readline()和input都是输入内容,区别在于:
- sys.stdin.readline()读取所有字符,包括结尾的换行符
- sys.stdin.readline()可以设置单次读取的字符个数,iinput不行
- sys.stdin.readline()不能设置提示信息,input可以
python中in的底层实现_python中print和input的底层实现相关推荐
- Python基础_第3章_Python中的循环结构
Python基础_第3章_Python中的循环结构 文章目录 Python基础_第3章_Python中的循环结构 Python中的循环结构 一.回顾分支练习题 1.判断是否为一个合法三角形 2.求世界 ...
- Python基础_第5章_Python中的数据序列
Python基础_第5章_Python中的数据序列 文章目录 Python基础_第5章_Python中的数据序列 Python中的数据序列 一.字典--Python中的==查询==神器 1.为什么需要 ...
- python字典是什么的集合_Python中的字典和集合
文章目录字典1.介绍 2.创建字典 3.字典内元素的访问 4.字典元素的添加.修改.删除 5.关于字典的其它操作 6.字典存储底层原理 集合1.介绍 2.相关操作 3.集合元素的添加 4.移除元素 字 ...
- python中的变量的引用_python中的变量引用小结
python的变量都可以看成是内存中某个对象的引用.(变量指向该内存地址存储的值) 1.python中的可更改对象和不可更改对象 python中的对象可以分为可更改(mutable)对象与不可更改(i ...
- python post与get的区别_python中get和post有什么区别
更多编程教程请到:菜鸟教程 https://www.piaodoo.com/ 友情链接: 高州阳光论坛https://www.hnthzk.com/ 人人影视http://www.sfkyty.com ...
- python中pow是什么函数_python中pow什么意思
python中pow什么意思? python中pow() 方法返回 xy(x的y次方) 的值. 语法 以下是 math 模块 pow() 方法的语法:import math math.pow( x, ...
- python中全局变量和局部变量关键字_Python中全局变量和局部变量的理解与区别
前言 学过编程的人应该对全局变量与局部变量这两个名词并不陌生,Python也同多数编程语言一样,也有全局变量与局部变量的概念 但是与其他编程语言又有所不同 全局变量与局部变量两者的本质区别就是在于作用 ...
- python深复制与浅复制_Python中的深拷贝和浅拷贝详解
要说清楚Python中的深浅拷贝,需要搞清楚下面一系列概念: 变量-引用-对象(可变对象,不可变对象)-切片-拷贝(浅拷贝,深拷贝) [变量-对象-引用] 在Python中一切都是对象,比如说:3, ...
- python运行mcmc为何老出错_python中mcmc方法的实现
MCMC方法在贝叶斯统计中运用很多,MIT发布的EMCEE是实现的比较好的.介绍页面在下面.源代码中examples里的代码可以帮助理解各种功能,特别是line.py 列出了最小二乘法,最大似然法和M ...
- python实例属性与类属性_Python中的类属性和实例属性引发的一个坑-续
上篇文章Python 中的类属性和实例属性,我们探讨了类属性和实例属性引发的一个小坑.总结了类属性和实例属性的区别如下:类属性可以被类和实例调用,实例属性只能被实例调用 类属性不会随着实例的调用而改变 ...
最新文章
- Excel 计算除法并显示为万分之几,如0.15‱
- mysql教程左右链接_mysql的左右内连接用法实例
- Opengl-深度测试模板测试
- 树莓派应用实例5:测量土壤湿度
- main函数的参数argc和argv
- [JSOI2008]Blue Mary的战役地图——全网唯一一篇dp题解
- NIO学习–核心概念与基本读写
- idea教程--Maven 骨架介绍
- aix vnc oracle,请问如何配置AIX上的vnc
- 我们的小窝-情侣空间秀恩爱php源码
- linux make使用技巧
- Android应用开发(11)---动画资源
- c语言调用子程序的方式,哪位师傅知道51单片机怎样编写子程序?C语言的。在主程序里调...
- 感受一下Java中的“枚举类型”
- (转)pb控制打印机
- 软件程序设计中的N-S图、PAD图、程序流程图、E-R图
- 数学方法论的含义和研究意义
- CAD图纸的保存格式DWG DWF DXF互相转换是怎么操作的?
- oracle+standard+cost,成本管理系统
- K8s Mysql 一主多从 主机宕机的解决方法