python-3.x - 如何在Python 3中的同一行中打印多种不同类型颜色的文本?

标签 python-3.x

我正在尝试在终端上做一些可视化数据,并且我正在做大量的打印来做到这一点。我遇到的问题是某些字符符号看起来相同。我认为给它们涂上不同的颜色可以帮助我看到差异。我从this link看到有一种方法可以做到这一点,但我不明白我在看什么。没有解释这些解决方案中发生的情况。

如何在同一行中指定不同颜色的文本? 我还应该提到,我正在构建对 print() 调用的可打印字符串 OUTSIDE。如何在 print() 调用之外构建彩色字符串?

我的问题的重写形式:

1. Colorize the text I print to the string with multiple types of colors.
2. Colorize the string BEFORE it gets sent to the "print()" call.

几个例子就太好了。

最佳答案

使用colorama就像您链接的答案正在做的那样:

from colorama import Fore, Style

my_str = f"{Fore.BLUE}Hello, {Style.RESET_ALL} guys. {Fore.RED} I should be red."

print(my_str)

这给了我:

this output

如您所见Fore.<color name>更改其后文本的颜色,直到 Style.RESET_ALL 。之后您可以再次更改文本的颜色。

关于python-3.x - 如何在Python 3中的同一行中打印多种不同类型颜色的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60006808/

相关文章:

python - 这与字典有关

python - 从普通函数调用生成器函数

python - 两只随机行走的 turtle 的无限循环

python argparse : allow unregistered arguments

python - python中的目录文件导入错误

Python打印函数

python - 如何从给定路径中删除 ..

python - plotly/破折号 : is it possible to hide tick labels of a secondary y axis?

python - 正则表达式捕捉网址

python - 找不到满足 pyyaml>=5.3 要求的版本,但 PyYAML 是版本 5.3.1