有没有一种方法可以在不使用\ b的情况下打印出python的backpace?

发布于 2025-01-25 11:24:50 字数 115 浏览 1 评论 0原文

我想发挥打字效果,但我无法打印回头台。我尝试使用print('\ b'),但它只是打印出一个X-ed Out Square。还有其他打印靠背的方法吗?如果有助于我将MU用作我的代码编辑器。

I want to make a typing effect but I can't print a backspace. I tried using print('\b') but it just printed out an x-ed out square. Are there any other ways to print a backspace? If it helps I'm using Mu as my code editor.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满栀 2025-02-01 11:24:50

问题是在您的IDE中运行脚本,其终端(显然)不支持擦除字符(在此并不罕见;使用Python发货的闲置IDE也不支持输出中的背景)。在常规终端(例如cmd.exe)在Windows上运行,bash或标准 *nix终端上的like)和打印'\ b'<'\ b'< /代码>工作。这全都取决于终端如何支持(或不支持)backpacing。

为了记录,您要使用:

 print('\b', end='')  # Optionally add flush=True to force it out immediately

或:

 sys.stdout.write('\b')
 # Optionally:
 sys.stdout.flush()  # to force it out immediately

避免尾随的新线(在角色上进行backpace不应将您迫使您作为副作用的下一行)。

The problem is running the script in your IDE, whose terminal (apparently) doesn't support backspacing to erase characters (it's not unusual in this; the IDLE IDE that ships with Python doesn't support backspaces in output either). Run it in a regular terminal (e.g. cmd.exe on Windows, bash or the like on a standard *NIX terminal), and printing '\b' works. It's all up to the terminal how it supports (or doesn't support) backspacing.

For the record, you'd want to use either:

 print('\b', end='')  # Optionally add flush=True to force it out immediately

or:

 sys.stdout.write('\b')
 # Optionally:
 sys.stdout.flush()  # to force it out immediately

to avoid the trailing newline (backspacing over a character shouldn't force you to the next line as a side-effect).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文