Python之'\r'

来源:互联网 发布:淘宝超级品牌日 编辑:程序博客网 时间:2024/06/05 21:01

今天练习print时,遇到一个问题:

程序一:


程序二:



程序三:



问题:5,6行代码在程序一与程序二中都没有输出。

为什么?

我刚开始以为格式控制符,或者字符串问题,或者for语句问题,后来发现都不是。

最后锁定到第六行。

 '\r' 大家都很熟悉,不就是回车符嘛。

但是注意,这个回车符,可跟'\n'不太一样,后者是换行符。


\nASCII linefeed (LF)

\rCarriage Return (CR)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'将光标移到开始位置,输出第二个字符,覆盖第一个,

就这样下去,直至到最后一个字符。

0 0
原创粉丝点击