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程序运行效率的6个方法
Mar 31 Python
在Linux中通过Python脚本访问mdb数据库的方法
May 06 Python
Python中unittest模块做UT(单元测试)使用实例
Jun 12 Python
深入解析Python设计模式编程中建造者模式的使用
Mar 02 Python
python采集微信公众号文章
Dec 20 Python
python调用百度地图WEB服务API获取地点对应坐标值
Jan 16 Python
django表单的Widgets使用详解
Jul 22 Python
在 Jupyter 中重新导入特定的 Python 文件(场景分析)
Oct 27 Python
基于python3生成标签云代码解析
Feb 18 Python
python爬取代理IP并进行有效的IP测试实现
Oct 09 Python
python xlwt模块的使用解析
Apr 13 Python
python百行代码实现汉服圈图片爬取
Nov 23 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
浅析is_writable的php实现
2013/06/18 PHP
php setcookie(name, value, expires, path, domain, secure) 参数详解
2013/06/28 PHP
php魔术方法功能与用法实例分析
2016/10/19 PHP
php基于ob_start(ob_gzhandler)实现网页压缩功能的方法
2017/02/18 PHP
js querySelector和getElementById通过id获取元素的区别
2012/04/20 Javascript
关于js中for in的缺陷浅析
2013/12/02 Javascript
jquery实现效果比较好的table选中行颜色
2014/03/25 Javascript
元素未显示设置width/height时IE中使用currentStyle获取为auto
2014/05/04 Javascript
chrome下jq width()方法取值为0的解决方法
2014/05/26 Javascript
node.js中的fs.readdirSync方法使用说明
2014/12/17 Javascript
九种原生js动画效果
2015/11/11 Javascript
浅析AngularJS中的指令
2016/03/20 Javascript
基于Jquery插件Uploadify实现实时显示进度条上传图片
2020/03/26 Javascript
JS库之ParticlesJS使用简介
2017/09/12 Javascript
基于复选框demo(分享)
2017/09/27 Javascript
前端必备插件之纯原生JS的瀑布流插件Macy.js
2017/11/22 Javascript
用图片替换checkbox原始样式并实现同样的功能
2018/11/15 Javascript
jQuery实现文本显示一段时间后隐藏的方法分析
2019/06/20 jQuery
javascript自定义日期比较函数用法示例
2019/07/22 Javascript
vue中后端做Excel导出功能返回数据流前端的处理操作
2020/09/08 Javascript
python的迭代器与生成器实例详解
2014/07/16 Python
Python解析excel文件存入sqlite数据库的方法
2016/11/15 Python
python爬虫爬取某站上海租房图片
2018/02/04 Python
Python 实现自动导入缺失的库
2019/10/29 Python
tensorflow 限制显存大小的实现
2020/02/03 Python
Python基于进程池实现多进程过程解析
2020/04/30 Python
python3.7添加dlib模块的方法
2020/07/01 Python
Python2与Python3关于字符串编码处理的差别总结
2020/09/07 Python
Blue Nile蓝色尼罗河香港官网:世界最大在线钻石珠宝销售商
2020/05/07 全球购物
为什么使用接口?
2014/08/13 面试题
某个公司的Java笔面试题
2016/03/11 面试题
模特职业生涯规划范文
2014/02/26 职场文书
大学生感恩父母演讲稿
2014/08/28 职场文书
王金山在党的群众路线教育实践活动总结大会上的讲话稿
2014/10/25 职场文书
2016廉政教育学习心得体会
2016/01/25 职场文书
Java 多线程并发FutureTask
2022/06/28 Java/Android