在python终端中打印颜色的3中方式(python3经典编程案例)
在 Python 中有几种方法可以将彩色文本输出到终端。 最常见的做法是:
1、使用内置模块:colorama
模块
可以使用 Colorama 的 ANSI 转义序列的常量简写来完成彩色文本的跨平台打印:
案例1:
from colorama import Fore, Back, Styleprint(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, cprinttext = 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")
在python终端中打印颜色的3中方式(python3经典编程案例)相关推荐
- 写出高效率python的90个方法,附案例(python3经典编程案例)
Effective Python摘录. 一. 培养Pythonic思维 1. 查询自己使用的python版本 import sys print(sys.version_info) print(sys. ...
- python第三方库-字符串编码工具 chardet 的使用(python3经典编程案例)
一. chardet介绍 chardet这个第三方库的使用非常容易,chardet支持检测中文.日文.韩文等多种语言. 字符串编码一直是令人非常头疼的问题,尤其是我们在处理一些不规范的第三方网页的时候 ...
- 编程语言用Python实现九九乘法表的几种方式,入门必备案例!超级简单!
编程语言用Python实现九九乘法表的几种方式,入门必备案例!超级简单! 我们在学习Python的过程中需要不断的积累和练习,这样才能够走的更远,今天一起来学习怎么用Python写九九乘法表~ 第一种 ...
- iOS asset 中定义颜色,xib中便捷访问
在aseet 中定义一个颜色 这样就可以在xib 中访问颜色了,这样就不用重复的去输入
- python画三维几何图形拼成的图案_Scratch3.0少儿编程案例:循环画窗花
一.案例简介 窗花艺术是我国非物质文化遗产,每逢过年过节的时候,家家户户都在窗户上贴满窗花.今天我们就来用scratch 3.0来制作少儿编程案例:循环画窗花.简单的窗花图案都是对称的,由多个重复的简 ...
- python的print打印颜色设置
1.顺序:显示方式,前景颜色,背景颜色 2.顺序非固定,但尽量按默认书写方式 3.也可以在input中输出使用 4.格式: print('\033[显示方式:前景颜色:背景颜色m .......... ...
- html中取颜色快捷键,ps中填充颜色的快捷键是什么?
photoshop填充颜色的快捷键:填充前景色:Alt+Delete 填充背景色:Ctrl+Delete 在Photoshop中前景色和背景色只是一个概念问题,是Photoshop为我们提供的2种快捷 ...
- substrate中打印调试信息的多种方式详解
目录 1. 获取substrate-node-template代码 2. 添加一个用于测试的pallet至依赖到pallets目录 3. log方式来输出信息 3.1 将log依赖添到cargo.to ...
- 用python实现打印各种图形总结(包括等腰三角形,菱形,近似圆等)(Python经典编程案例)
1. 打印等腰三角形 lines = int(input("输入要打印的行数:")) for i in range(lines):for j in range(0, lines - ...
最新文章
- 跟谁学这2年间做的六件事
- java使用正则表达为数字添加千位符的简单方法
- 使用vue的mixins混入实现对正在编辑的页面离开时提示
- mnn linux编译
- 到底应该加几个卷积核?
- JavaScript创建命名空间
- Kotlin开发细节
- (转载)arcgis for js - 解决加载天地图和WMTS服务,WMTS服务不显示的问题,以及wmts服务密钥。...
- matplotlib 柱状图 分组_Python数据分析与可视化之matplotlib可视化(三)
- 线性回归的梯度下降和正规方程组求解
- GCC为什么不将a * a * a * a * a * a优化为(a * a * a)*(a * a * a)?
- 【堆】Leetcode692:前k个高频单词
- php源码 备课系统,电子教案管理系统2012版 V3.2
- 仿映客直播礼物特效制作流程
- DTcms-【需求】-需求分析
- 听见丨三星Bixby中文(普通话)版正式发布 云端服务商Scalyr获2000万美元A轮融资
- vulnstack4渗透-内网篇
- C语言中的文件操作(二)
- R语言数据类型基础1 数值型、逻辑型与文本型
- 视觉中国财报背后的版权门余波