Python通过2种方法输出带颜色字体


Posted in Python onMarch 02, 2020

方法1:

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

书写语法

print(\033[显示方式;前景色;背景色m输出内容\033[0m)

其中,显示方式、前景色、背景色都是可选参数(可缺省一个或多个)。

参数

显示方式

显示方式 效果
0 默认
1 粗体
4 下划线
5 闪烁
7 反白显示
print("显示方式:")
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")

Python通过2种方法输出带颜色字体

颜色

字体色编号 背景色编号 颜色
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通过2种方法输出带颜色字体

方法2:

colorama是一个python专门用来在控制台、命令行输出彩色文字的模块,可以跨平台使用。

1. 安装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

跨平台印刷彩色文本可以使用彩色光的常数简称ANSI转义序列:

from colorama import Fore,Back,Style
print (Fore.RED + "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")

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中的yield浅析
Jun 16 Python
在Python的Django框架中包装视图函数
Jul 20 Python
python制作爬虫并将抓取结果保存到excel中
Apr 06 Python
实现python版本的按任意键继续/退出
Sep 26 Python
Python调用C# Com dll组件实战教程
Oct 12 Python
selenium+python自动化测试之多窗口切换
Jan 23 Python
基于腾讯云服务器部署微信小程序后台服务(Python+Django)
May 08 Python
python 随机森林算法及其优化详解
Jul 11 Python
Python 用matplotlib画以时间日期为x轴的图像
Aug 06 Python
利用OpenCV和Python实现查找图片差异
Dec 19 Python
pytorch 求网络模型参数实例
Dec 30 Python
浅谈django 模型类使用save()方法的好处与注意事项
Mar 28 Python
Python实现屏幕录制功能的代码
Mar 02 #Python
python实现录屏功能(亲测好用)
Mar 02 #Python
基于Numba提高python运行效率过程解析
Mar 02 #Python
Python3 assert断言实现原理解析
Mar 02 #Python
Django认证系统user对象实现过程解析
Mar 02 #Python
在python中使用pymysql往mysql数据库中插入(insert)数据实例
Mar 02 #Python
Python基于requests库爬取网站信息
Mar 02 #Python
You might like
PHP file_exists问题杂谈
2012/05/07 PHP
配置php网页显示各种语法错误
2013/09/23 PHP
php中curl使用指南
2015/02/05 PHP
在html文件中也可以执行php语句的方法
2015/04/09 PHP
Django中的cookie与session操作实例代码
2017/08/17 PHP
laravel 实现向公共模板中传值 (view composer)
2019/10/22 PHP
laravel框架模型和数据库基础操作实例详解
2020/01/25 PHP
一个js写的日历(代码部分网摘)
2009/09/20 Javascript
JS 图片缩放效果代码
2010/06/09 Javascript
ExtJs 表单提交登陆实现代码
2010/08/19 Javascript
js 延迟加载 改变JS的位置加快网页加载速度
2012/12/11 Javascript
js导航栏单击事件背景变换示例代码
2014/01/13 Javascript
javascript中caller和callee详解
2015/08/10 Javascript
AngularJS ng-template寄宿方式用法分析
2016/11/07 Javascript
利用node.js写一个爬取知乎妹纸图的小爬虫
2017/05/03 Javascript
input框中自动展示当前日期yyyy/mm/dd的实现方法
2017/07/06 Javascript
利用Vue实现移动端图片轮播组件的方法实例
2017/08/23 Javascript
js构造函数创建对象是否加new问题
2018/01/22 Javascript
JavaScript实现页面中录音功能的方法
2019/06/04 Javascript
vue 强制组件重新渲染(重置)的两种方案
2019/10/29 Javascript
vue中可编辑树状表格的实现代码
2020/10/31 Javascript
利用Python进行异常值分析实例代码
2017/12/07 Python
python Crypto模块的安装与使用方法
2017/12/21 Python
Python matplotlib以日期为x轴作图代码实例
2019/11/22 Python
python实现将两个文件夹合并至另一个文件夹(制作数据集)
2020/04/03 Python
Python-jenkins 获取job构建信息方式
2020/05/12 Python
Python 高效编程技巧分享
2020/09/10 Python
Python中return函数返回值实例用法
2020/11/19 Python
美国眼镜在线零售商:Dualens
2019/12/07 全球购物
卫校护理专业毕业生求职信
2013/11/26 职场文书
宣传策划类求职信范文
2014/01/31 职场文书
女子职高个人自荐书
2014/02/01 职场文书
《学会待客》教学反思
2014/02/22 职场文书
2016年公务员六五普法心得体会
2016/01/21 职场文书
Win11电源已接通但未充电怎么办?Win11电源已接通未充电的解决方法
2022/04/05 数码科技
docker-compose部署Yapi的方法
2022/04/08 Servers