首页 > 解决方案 > Python 3.x: Why idle prints escape character \r as ♪?

问题描述

idle prints \r as ♪</p>

Code

print('haha', end='\r')

print('haha\rhehe')

This is output

haha♪

haha♪hehe
#

Thanks for finding another same question! Is there any possible way to use \r? There is no explain about this...

标签: pythonpython-idle

解决方案


默认情况下,python(不是 IDLE)将打印输出发送到 sys.stdout。IDLE 将 sys.stdout 连接到 tinter (tcl/tk) Text 小部件。然后会发生什么取决于 tk、操作系统和您使用的字体。对于制表符 ('\t') 和输入 ('\n') 以外的控制字符,结果未定义。可能性包括什么都没有、一个空格和一个令人费解的符号字符。使用带有 SourceCodePro 字体的 Win 10 上的 3.7.3,我什么也看不到。 print('a\rb')结果ab。IDLE 文档,Help ==> IDLE 帮助,Shell 中的用户输出部分讨论了控制字符的打印。

要查看如果您在特定终端或控制台中直接使用 python 运行代码会发生什么,请在该特定环境中运行它。要在不考虑操作系统的情况下详细控制输出,请使用 gui 框架,例如 tkinter。


推荐阅读