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. 「UVA10766」Organising the Organisation(生成树计数)

    BUPT 2017 Summer Training (for 16) #6C 题意 n个点,完全图减去m条边,求生成树个数. 题解 注意可能会给重边. 然后就是生成树计数了. 代码 #include ...

  2. Docker 私有仓库 Harbor registry 安全认证搭建 [Https]

    Harbor源码地址:https://github.com/vmware/harborHarbort特性:基于角色控制用户和仓库都是基于项目进行组织的, 而用户基于项目可以拥有不同的权限.基于镜像的复 ...

  3. 【dfs】LETTERS

    1212:LETTERS [题目描述] 给出一个roe×colroe×col的大写字母矩阵,一开始的位置为左上角,你可以向上下左右四个方向移动,并且不能移向曾经经过的字母.问最多可以经过几个字母. [ ...

  4. centos7安装部署本地局域网yum源

    应用场景: 当Linux系统都是最小化安装的系统,又无法做到每台都能访问外网的情况下,安装常用工具或者依赖包的最好办法可能就是建立本地yum源了. 安装环境: 一台 centos 7.4 minima ...

  5. Jupyter-Notebook 删除指定 kernel

    原来是Python3+C# 查看列表jupyter kernelspec list 删除指定kernel:jupyter kernelspec remove icsharpkernel 删除成功:(刷 ...

  6. Linux命令模拟Http的get或post请求

    Http请求指的是客户端向服务器的请求消息,Http请求主要分为get或post两种,在Linux系统下可以用curl和wget命令来模拟Http的请求. get请求: 1.使用curl命令: cur ...

  7. UOJ176 新年的繁荣

    题目链接 Boruvka生成树算法 \(Boruvka\)算法就是先把每个点看作一个联通块,然后不断在联通块之间找最优的边进行合并.因为每次联通块的数量最少缩小一半.所以合并次数是\(log\)的 先 ...

  8. 在views中引用UserProfile报错RuntimeError: Model class apps.users.models.UserProfile doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

    如图报错,在settings中,该加的也加了啊! 显然类似于网上最容易遇到的解决方案如下图,是没有任何意义的 只要在view中有 from .models import UserProfile,Ver ...

  9. 1. github配置

    1. 安装:官网傻瓜式安装 2.密钥的生成:为了不让不想干的人提交代码,所以需要一个密钥 执行这个命令 : ssh-keygen -t rsa -C "邮箱地址" 然后一直回车键回 ...

  10. Codeforces Round #518 (Div. 2) B LCM

    传送门 https://www.cnblogs.com/violet-acmer/p/10163375.html 题解: 这道题有点意思,有点数学的味道. 根据定义“[a,b] / a”可得这求得是l ...