首页 > 解决方案 > 如何在控制台中使输入文本加粗?

问题描述

我正在寻找一种方法使用户在控制台中键入的文本变为粗体

input("Input your name: ")

如果我输入“John”,我希望它在我输入时显示为粗体,就像这样

输入您的姓名:约翰

标签: pythonpython-3.x

解决方案


它们被称为ANSI 转义序列。基本上你输出一些特殊的字节来控制终端文本的外观。尝试这个:

x = input('Name: \u001b[1m')  # anything from here on will be BOLD

print('\u001b[0m', end='')  # anything from here on will be normal
print('Your input is:', x)

\u001b[1m告诉终端切换到粗体文本。\u001b[0m告诉它重置。

本页很好地介绍了 ANSI 转义序列。


推荐阅读