gpt4 book ai didi

python - 如何使用python输出颜色?

转载 作者:行者123 更新时间:2023-11-30 22:53:49 24 4
gpt4 key购买 nike

我是 python 新手(大约一周或更短时间),我使用的是 python 2.7。

我正在编写一个程序来检查 ip 验证和类,并且我希望将输出设为彩色,以便用户在终端上更容易阅读。

我已经尝试过这个:

# to make the text purple for example.
print '\033[95m' + "This is a purple output"
# if i'm writing another simple print after the first one it will be purple too
print "Example" # now this statement purple too

但是当我使用这个示例时,第一个打印方法之后的输出也变成紫色。

感谢所有尝试的人。

最佳答案

通过打印转义码\033[0m,可以重置颜色:

>>> print '\033[95m' + "This is a purple output" + '\033[0m'
This is a purple output

或者,您可以使用 colorama package :

>>> from colorama import Fore
>>> print Fore.LIGHTMAGENTA_EX + "This is a purple output" + Fore.RESET
This is a purple output

关于python - 如何使用python输出颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38071537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com