python - Python 控制台中的粗体格式

标签 python console ansi output-formatting

我已经用 Python 代码定义了一个列表。

list = ['kumar','satheesh','rajan']    
BOLD = '\033[1m'

for i, item in enumerate(list):
   list[i] = BOLD + item

print (list)

但我得到的输出为 ['\x1b[1mfsdfs', '\x1b[1mfsdfsd', '\x1b[1mgdfdf']

但要求的输出是['kumar','satheesh','rajan']

如何使用python格式化list elementsbold

最佳答案

您还需要指定 END = '\033[0m':

list = ['kumar','satheesh','rajan']

BOLD = '\033[1m'
END = '\033[0m'

for each in list:
    print('{}{}{}'.format(BOLD, each, END))

要使列表本身像 ['kumar', 'satheesh', 'rajan'] 一样加粗:

print('{}{}{}'.format(BOLD, list, END))

关于python - Python 控制台中的粗体格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50460222/

相关文章:

Python,可变长度位置参数后的默认关键字参数

python - 从列表中删除空项 (Python)

linux - UART 绑定(bind)@运行时

c - ANSI C 从文件中分离数据

c - 在 C 中将大括号放在字符串中

python - 网络抓取数据的词形还原

python - groupby 在有序分类列上的奇怪行为

c# - 如何在 Windows 10 上的 UWP 中输出到控制台?

java - 从控制台的一行读取整数和字符串

c - 防止 C 键盘输入中的 ANSI 转义字符