print

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


<br>
## 小结
sys.stdout.write()和print都是向屏幕输出内容,区别在于:
- sys.stdout.write()没有自动换行,print有
- <b><font color='#ff0000'>sys.stdout.write()只能写入字符串</font></b>,print可以写入任意数据类型 <br>
## 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

为什么两个长度不一样呢?这是因为<b><font color='#ff0000'>sys.stdin.readline()把结尾的换行符也算进去了</font></b>,6和hello不在一行也可以证明这一点
<br>
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))

<br>
控制台输出结果
```python
hello
hello
6

当传入的参数为负数时,表示获取整行
```python
d = sys.stdin.readline(-3)
print(d, len(d))

<br>
控制台输出结果
```python
helloworld
helloworld
11

当一次没有读取完时,下一次读取将会从上次的结束位置开始,这一点与文件类似
```python
c = sys.stdin.readline(6)
print(c, len(c))

d = sys.stdin.readline(4)

print(d, len(d))

<br>
控制台输出结果
```python
helloworld
hellow 6
orld 4

sys.stdin.readline()不能像input一样传入字符串作为提示信息
```python
username = input("username:")

password = sys.stdin.readline("password:")

<br>
控制台输出结果
```python
username:robin
Traceback (most recent call last):
File "D:/input_print.py", line 50, in <module>
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中print和input的底层实现的更多相关文章

  1. Python中print()函数不换行的方法

    一.让print()函数不换行 在Python中,print()函数默认是换行的.但是,在很多情况下,我们需要不换行的输出(比如在算法竞赛中).那么,在Python中如何做到这一点呢? 其实很简单.只 ...

  2. python中print后面加逗号

    python中print输出一行,如果想多次输出的内容不换行,可以在print后面加逗号 例如 每个输出一行 phrase = "abcdefg" # Add your for l ...

  3. python中print()函数的“,”与java中System.out.print()函数中的“+”

    python中的print()函数和java中的System.out.print()函数都有着打印字符串的功能. python中: print("hello,world!") 输出 ...

  4. Python中print字体颜色的设置

    Python中print字体颜色的设置 实现过程:       终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关.       转义序列是以ESC开头,即用\033来完成 ...

  5. 【313】python 中 print 函数用法总结

    参考:python 中 print 函数用法总结 参考:Python print() 函数(菜鸟教程) 参考:Python 3 print 函数用法总结 目录: 字符串和数值类型 变量 格式化输出 p ...

  6. Python中print用法里面% ,"%s 和 % d" 代表的意思

    Python 编程 里面% . "%s 和 % d" 代表的意思 %s,表示格化式一个对象为字符 %d,整数 "Hello, %s"%"zhang3& ...

  7. python中raw_input() 与 input()

    参考网址:http://www.cnblogs.com/way_testlife/archive/2011/03/29/1999283.html 在python中如何接收一个输入的字符串. 举个例子: ...

  8. python 中 print 函数用法总结

    Python 思想: “一切都是对象!” 在 Python 3 中接触的第一个很大的差异就是缩进是作为语法的一部分,这和C++等其他语言确实很不一样,所以要小心 ,其中python3和python2中 ...

  9. Learning Python 002 print() 和 input()

    Python print() 和 input() print()函数 print()函数可以向终端中输入指定的内容. 输出当个字符串 .py文件中,输入下面的代码,并保存: print('hello ...

随机推荐

  1. 【C++】实现一个简单的单例模式

  2. 【C++】VS2015/VS2017连接Mysql数据库教程

    要给C++程序连接MySQL数据库,分别需要: 安装MySQL Server 下载MySQL Connector/C++ 在IDE中配置依赖 然后就可以在代码中调用API,来连接以及操作数据库. 一. ...

  3. 来一波全套向量运算(C++)

    //头文件要求 #include <cmath> struct P{long long x, y;}p[N]; //加法 P operator +(P x, P y){return (P) ...

  4. Linux 日志分析脚本

    #### 以下代码,若出现无法使用,请根据底下图片,更改参数.根据 apache 日志格式修改 查看 apache 进程ps aux | grep httpd | grep -v grep | wc ...

  5. Hdoj 1058.Humble Numbers 题解

    Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The ...

  6. django pymysql

    此处django版本为1.11.13 设置setting.py DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NA ...

  7. 51nod 1005 1027 1029 高精度

    Java大数用法参考:https://www.cnblogs.com/jin-nuo/p/5313205.html 1005 大数加法: import java.util.*; import java ...

  8. 从Java的角度修复文件下载漏洞

    从Java的角度谈下文件下载漏洞的产生,然后到他的修复方案.这里我的修复方案是白名单,而没有采用黑名单的方式. 首先先看一段存在文件下载漏洞的代码code: HTML视图页面  download.ht ...

  9. 三小时学会Kubernetes:容器编排详细指南

    三小时学会Kubernetes:容器编排详细指南 如果谁都可以在三个小时内学会Kubernetes,银行为何要为这么简单的东西付一大笔钱? 如果你心存疑虑,我建议你不妨跟着我试一试!在完成本文的学习后 ...

  10. 解题:SDOI2018 战略游戏

    题面 先圆方树然后建虚树,答案就是虚树大小.虚树没必要建出来,把原来的点的点权设为1,直接dfs序排序后相邻点求距离加上首尾两个点的距离,最后除以二(画一下可以发现是正反算了两遍),注意还要去掉询问点 ...