Python之'\r'
2016-03-29 11:53
363 查看
今天练习print时,遇到一个问题:
程序一:
![](http://img.blog.csdn.net/20160329113514952)
![](http://img.blog.csdn.net/20160329113327732)
程序二:
![](http://img.blog.csdn.net/20160329113128185)
![](http://img.blog.csdn.net/20160329113438045)
程序三:
![](http://img.blog.csdn.net/20160329113824297)
![](http://img.blog.csdn.net/20160329113526467)
问题:5,6行代码在程序一与程序二中都没有输出。
为什么?
我刚开始以为格式控制符,或者字符串问题,或者for语句问题,后来发现都不是。
最后锁定到第六行。
'\r' 大家都很熟悉,不就是回车符嘛。
但是注意,这个回车符,可跟'\n'不太一样,后者是换行符。
wiki : It commands a printer,
or other output system such as the display of a system
console,to move the position of the cursor to
the first position on the same line. It was mostly used along with line
feed (LF), a move to the next line, so that together they start a new line.
解释一下:它可以把光标移动到该行的开始位置,经常与换行符一起开始新的一行。
这个程序中还隐含这一点,Python的print自动开始新的一行,但是加了逗号,就不开始新的一行了。
第六行代码,加了逗号,故输出第一个字符后,不换行,'\r'将光标移到开始位置,输出第二个字符,覆盖第一个,
就这样下去,直至到最后一个字符。
程序一:
程序二:
程序三:
问题:5,6行代码在程序一与程序二中都没有输出。
为什么?
我刚开始以为格式控制符,或者字符串问题,或者for语句问题,后来发现都不是。
最后锁定到第六行。
'\r' 大家都很熟悉,不就是回车符嘛。
但是注意,这个回车符,可跟'\n'不太一样,后者是换行符。
\n | ASCII linefeed (LF) |
\r | Carriage Return (CR) |
or other output system such as the display of a system
console,to move the position of the cursor to
the first position on the same line. It was mostly used along with line
feed (LF), a move to the next line, so that together they start a new line.
解释一下:它可以把光标移动到该行的开始位置,经常与换行符一起开始新的一行。
这个程序中还隐含这一点,Python的print自动开始新的一行,但是加了逗号,就不开始新的一行了。
第六行代码,加了逗号,故输出第一个字符后,不换行,'\r'将光标移到开始位置,输出第二个字符,覆盖第一个,
就这样下去,直至到最后一个字符。
相关文章推荐
- 【python】list。列表
- Python使用taskkill 报错 系统找不到指定的路径
- 关于迅速上手Pychart的简单介绍
- 【Python】装饰器实现日志记录
- python调用父目录的模块
- numpy 基础 —— np.linalg
- Python Twisted介绍
- A Byte of Python 笔记(3)运算符和表达式
- python 文件操作
- python操作s3 -- boto2.x
- Python检测文本类型
- Python简单网页爬虫——极客学院视频自动下载
- Python学习笔记(五)OOP
- 02-python中文编码
- Python Basics
- 01-python开发环境搭建
- 《Python数据分析基础教程:Numpy学习指南》- 速记 - 第七章
- python 基础学习笔记(1)
- Python中sort函数、sorted函数和argsort函数
- Python中的字典排序