Python彩色输出(Colored Print)
2022/2/7 17:12:30
本文主要是介绍Python彩色输出(Colored Print),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Python 彩色输出
1.原理解析
在控制台输出彩色信息,不是编程语言特有的属性,而是利用了ANSI
转义序列。无论使用的是哪种编程语言,只要正确的使用ANSI
转义序列就可以打印出彩色信息。常用的ANSI
序列程序有ANSICON
等。
2.彩色输出方式一
直接使用ANSI
转义序列
如:
class bcolors: HEADER = '\033[95m' OKBLUE = '\033[94m' OKCYAN = '\033[96m' OKGREEN = '\033[92m' WARNING = '\033[93m' FAIL = '\033[91m' ENDC = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[4m'
使用
print(f"{bcolors.BOLD} this this {bcolors.ENDC}")
支持的效果有:
图片来自于ref1, ref2
-
浅色文字,深色背景
-
深色文字,浅色背景
-
闪烁效果
3.彩色输出方式二
使用colorama
或者termcolor
,譬如使用termcolor
:
>>> from termcolor import colored >>> print(colored("this is the dog king.", "red"))
Ref
1.https://blog.csdn.net/sigmarising/article/details/88765912
2.https://zh.wikipedia.org/wiki/ANSI%E8%BD%AC%E4%B9%89%E5%BA%8F%E5%88%97
3.https://stackoverflow.com/questions/287871/how-to-print-colored-text-to-the-terminal
这篇关于Python彩色输出(Colored Print)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python