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. Codeforces Round #517 Div. 2/Div. 1

    \(n\)天没更博了,因为被膜你赛的毒瘤题虐哭了... 既然打了这次CF还是纪念一下. 看看NOIP之前,接下来几场的时间都不好.这应该是最后一场CF了,差\(4\)分上紫也是一个遗憾吧. A 给一个 ...

  2. 自学Python之路-django

    自学Python之路-django 自学Python之路[第一回]:1.11.2 1.3

  3. push的时候报错:Permission denied (publickey)

    最近,在push的时候遇到一个问题,简单描述下过程(git客户端命令行操作) 我先在本地建了一个文件夹,mkdir dubbodemo 然后进入到这个文件夹,cd dubbodemo 添加我的内容 初 ...

  4. MOSFET的半桥驱动电路设计要领详解

    1 引言 MOSFET凭开关速度快.导通电阻低等优点在开关电源及电机驱动等应用中得到了广泛应用.要想使MOSFET在应用中充分发挥其性能,就必须设计一个适合应用的最优驱动电路和参数.在应用中MOSFE ...

  5. Python By 360、小米

    小米 乱谈Python并发 说实话,我一直觉得PHP真的是最好的语言,不仅养活了一大批PHP程序员,同时还为安全人员提供了大量的就业机会.然而,令人唏嘘的是,安全界很多人其实是吃着Python的饭,操 ...

  6. ORACLE报错和解决方案

    ORA-01034: ORACLE not available ORA-27101 出现ORA-01034和ORA-27101的原因是多方面的:主要是oracle当前的服务不可用,shared mem ...

  7. cf 990G - GCD Counting

    题意 #include<bits/stdc++.h> #define t 200000 #define MAXN 200100 using namespace std; int n; in ...

  8. bzoj2208 连通数(bitset优化传递闭包)

    题目链接 思路 floyd求一下传递闭包,然后统计每个点可以到达的点数. 会tle,用bitset优化一下.将floyd的最后一层枚举变成bitset. 代码 /* * @Author: wxyww ...

  9. touch-paint

    效果如下 代码如下: //index.html <!DOCTYPE html> <html lang="zh-CN"> <head> <m ...

  10. semantic ui框架学习笔记二

    评论组件 文档里的评论组件介绍的比较清晰.这里我就挑一个我喜欢的格式展示出来: <div class="ui comments"> <h3 class=" ...