在python终端中打印颜色的3中方式(python3经典编程案例)

简介: 这篇文章介绍了在Python终端中打印彩色文本的三种方式:使用`colorama`模块、`termcolor`模块和ANSI转义码。

在 Python 中有几种方法可以将彩色文本输出到终端。 最常见的做法是:

1、使用内置模块:colorama 模块

可以使用 Colorama 的 ANSI 转义序列的常量简写来完成彩色文本的跨平台打印:
案例1:

from colorama import Fore, Back, Style

print(Fore.RED + 'some red text')
print(Fore.YELLOW + 'some red text')
print(Fore.BLUE + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')

案例二:

# colorama是一个python专门用来在控制台、命令行输出彩色文字的模块,可以跨平台使用。
# 安装colorama模块: pip install colorama
# 常用格式: Fore: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
# Back: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
# Style: DIM, NORMAL, BRIGHT, RESET_ALL
from colorama import Fore,Back,Style
print (Fore.RED + "some red text")
print (Fore.GREEN + "some red text")
print (Fore.YELLOW + "some red text")
print (Fore.BLUE + "some red text")
print (Fore.MAGENTA + "some red text")
print (Fore.CYAN + "some red text")
print (Fore.RESET + "some red text")
print (Fore.WHITE + "some red text")
print (Fore.BLACK + "some red text")
print (Back.GREEN + "and with a green background")
print (Style.DIM + "and in dim text")
print (Style.RESET_ALL)
print ("back to normal now!!")

# Init关键字参数: init()接受一些* * kwargs覆盖缺省行为  init(autoreset = False):
# 如果你发现自己一再发送重置序列结束时关闭颜色变化每一个打印,然后init(autoreset = True)将自动化。 示例:
from colorama import init,Fore
init(autoreset=True)
print (Fore.RED + "welcome to python !!")
print ("automatically back to default color again")

2、使用termcolor模块:

termcolor 是一个 Python 模块,用于在终端中输出 ANSII 颜色格式。

# Python program to print
# colored text and background
import sys
from termcolor import colored, cprint

text = colored('Hello, World!', 'red', attrs=['reverse', 'blink'])
print(text)
cprint('Hello, World!', 'green', 'on_red')

print_red_on_cyan = lambda x: cprint(x, 'red', 'on_cyan')
print_red_on_cyan('Hello, World!')
print_red_on_cyan('Hello, Universe!')

for i in range(10):
    cprint(i, 'magenta', end=' ')

cprint("Attention!", 'red', attrs=['bold'], file=sys.stderr)

3、使用 ANSI 转义码

打印彩色文本最常用的方法是直接打印 ANSI 转义序列。 这可以以不同的格式交付,例如:

构建要调用的函数:我们可以构建函数来调用特定颜色命名的函数来执行相关的 ANSI 转义序列。
案例一:

# 一. 使用Python中自带的print输出带有颜色或者背景的字符串

# 其中,显示方式、前景色、背景色都是可选参数(可缺省一个或多个)。
print('\033[显示方式;前景色;背景色m输出内容\033[0m')
print(f'\033[31m5. ---zhangjskf ---\033[0m ')


print("显示方式:")
# 显示方式    效果
# 0    默认
# 1    粗体
# 4    下划线
# 5    闪烁
# 7    反白显示
print("\033[0mSuixinBlog: https://suixinblog.cn\033[0m")
print("\033[1mSuixinBlog: https://suixinblog.cn\033[0m")
print("\033[4mSuixinBlog: https://suixinblog.cn\033[0m")
print("\033[5mSuixinBlog: https://suixinblog.cn\033[0m")
print("\033[7mSuixinBlog: https://suixinblog.cn\033[0m")


# 字体色编号    背景色编号    颜色
# 30    40    黑色
# 31    41    红色
# 32    42    绿色
# 33    43    黄色
# 34    44    蓝色
# 35    45    紫色
# 36    46    青色
# 37    47    白色
print("字体色:")
print("\033[30mSuixinBlog: https://suixinblog.cn\033[0m")
print("\033[31mSuixinBlog: https://suixinblog.cn\033[0m")
print("\033[32mSuixinBlog: https://suixinblog.cn\033[0m")
print("\033[4;33mSuixinBlog: https://suixinblog.cn\033[0m")
print("\033[34mSuixinBlog: https://suixinblog.cn\033[0m")
print("\033[1;35mSuixinBlog: https://suixinblog.cn\033[0m")
print("\033[4;36mSuixinBlog: https://suixinblog.cn\033[0m")
print("\033[37mSuixinBlog: https://suixinblog.cn\033[0m")
print("背景色:")
print("\033[1;37;40m\tSuixinBlog: https://suixinblog.cn\033[0m")
print("\033[37;41m\tSuixinBlog: https://suixinblog.cn\033[0m")
print("\033[37;42m\tSuixinBlog: https://suixinblog.cn\033[0m")
print("\033[37;43m\tSuixinBlog: https://suixinblog.cn\033[0m")
print("\033[37;44m\tSuixinBlog: https://suixinblog.cn\033[0m")
print("\033[37;45m\tSuixinBlog: https://suixinblog.cn\033[0m")
print("\033[37;46m\tSuixinBlog: https://suixinblog.cn\033[0m")
print("\033[1;30;47m\tSuixinBlog: https://suixinblog.cn\033[0m")

案例二:

# Python program to print
# colored text and background
def prRed(skk): print("\033[91m {}\033[00m".format(skk))


def prGreen(skk): print("\033[92m {}\033[00m".format(skk))


def prYellow(skk): print("\033[93m {}\033[00m".format(skk))


def prLightPurple(skk): print("\033[94m {}\033[00m".format(skk))


def prPurple(skk): print("\033[95m {}\033[00m".format(skk))


def prCyan(skk): print("\033[96m {}\033[00m".format(skk))


def prLightGray(skk): print("\033[97m {}\033[00m".format(skk))


def prBlack(skk): print("\033[98m {}\033[00m".format(skk))


prCyan("Hello World, ")
prYellow("It's")
prGreen("Geeks")
prRed("For")
prGreen("Geeks")
相关文章
|
7月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
1084 102
|
6月前
|
数据采集 监控 数据库
Python异步编程实战:爬虫案例
🌟 蒋星熠Jaxonic,代码为舟的星际旅人。从回调地狱到async/await协程天堂,亲历Python异步编程演进。分享高性能爬虫、数据库异步操作、限流监控等实战经验,助你驾驭并发,在二进制星河中谱写极客诗篇。
Python异步编程实战:爬虫案例
|
6月前
|
Python
Python编程:运算符详解
本文全面详解Python各类运算符,涵盖算术、比较、逻辑、赋值、位、身份、成员运算符及优先级规则,结合实例代码与运行结果,助你深入掌握Python运算符的使用方法与应用场景。
437 3
|
6月前
|
数据处理 Python
Python编程:类型转换与输入输出
本教程介绍Python中输入输出与类型转换的基础知识,涵盖input()和print()的使用,int()、float()等类型转换方法,并通过综合示例演示数据处理、错误处理及格式化输出,助你掌握核心编程技能。
645 3
|
6月前
|
并行计算 安全 计算机视觉
Python多进程编程:用multiprocessing突破GIL限制
Python中GIL限制多线程性能,尤其在CPU密集型任务中。`multiprocessing`模块通过创建独立进程,绕过GIL,实现真正的并行计算。它支持进程池、队列、管道、共享内存和同步机制,适用于科学计算、图像处理等场景。相比多线程,多进程更适合利用多核优势,虽有较高内存开销,但能显著提升性能。合理使用进程池与通信机制,可最大化效率。
451 3
|
6月前
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
641 0
|
JavaScript Python
【Python】Python3之i18n
最近在完成阿里云MVP共创任务定pgAdmin4定制任务的时候,接触到了Python的本地化与国际化,了解了Python多语言化的基本知识,记录一下分享。其中涉及Python基础类库gettext,大家可访问link。
1626 0
|
7月前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
439 104
|
7月前
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
345 103
|
7月前
|
机器学习/深度学习 人工智能 数据挖掘
Python:现代编程的首选语言
Python:现代编程的首选语言
312 82

推荐镜像

更多
下一篇
开通oss服务