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实现视频下载功能
Mar 14 Python
不管你的Python报什么错,用这个模块就能正常运行
Sep 14 Python
tesserocr与pytesseract模块的使用方法解析
Aug 30 Python
分享PyCharm的几个使用技巧
Nov 10 Python
Python 根据数据模板创建shapefile的实现
Nov 26 Python
python 用 xlwings 库 生成图表的操作方法
Dec 22 Python
Python 日期的转换及计算的具体使用详解
Jan 16 Python
python多线程实现同时执行两个while循环的操作
May 02 Python
Python如何给你的程序做性能测试
Jul 29 Python
Python的collections模块真的很好用
Mar 01 Python
python编写五子棋游戏
May 25 Python
matplotlib画混淆矩阵与正确率曲线的实例代码
Jun 01 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
如何实现给定日期的若干天以后的日期
2006/10/09 PHP
深入apache host的配置详解
2013/06/09 PHP
PHP图像处理类库及演示分享
2015/05/17 PHP
js 浏览本地文件夹系统示例代码
2013/10/24 Javascript
使用GruntJS构建Web程序之构建篇
2014/06/04 Javascript
node.js中的fs.fchown方法使用说明
2014/12/16 Javascript
javascript学习总结之js使用技巧
2015/09/02 Javascript
JavaScript程序中的流程控制语句用法总结
2016/05/23 Javascript
zepto与jquery的区别及zepto的不同使用8条小结
2016/07/28 Javascript
微信小程序侧边栏滑动特效(左右滑动)
2017/01/23 Javascript
JS实现批量上传文件并显示进度功能
2017/06/27 Javascript
如何在vue中使用ts的示例代码
2018/02/28 Javascript
vue弹窗组件使用方法
2018/04/28 Javascript
AngularJS中的作用域实例分析
2018/05/16 Javascript
vue实现动态添加数据滚动条自动滚动到底部的示例代码
2018/07/06 Javascript
在vue中多次调用同一个定义全局变量的实例
2018/09/25 Javascript
express如何解决ajax跨域访问session失效问题详解
2019/06/20 Javascript
vue3修改link标签默认icon无效问题详解
2019/10/09 Javascript
[02:07]2017国际邀请赛中国区预选赛直邀战队前瞻
2017/06/23 DOTA
Python的Flask框架应用程序实现使用QQ账号登录的方法
2016/06/07 Python
python 简单的绘图工具turtle使用详解
2017/06/21 Python
Python3中的json模块使用详解
2018/05/05 Python
记一次python 内存泄漏问题及解决过程
2018/11/29 Python
python 获得任意路径下的文件及其根目录的方法
2019/02/16 Python
Opencv实现抠图背景图替换功能
2019/05/21 Python
Django中的cookie和session
2019/08/27 Python
python自动生成证件号的方法示例
2021/01/14 Python
高清屏中使用Canvas绘图出现模糊的问题及解决方法
2019/06/03 HTML / CSS
一篇.NET面试题
2014/09/29 面试题
舞蹈专业求职信
2014/06/13 职场文书
尊老爱亲美德少年事迹材料
2014/08/14 职场文书
学生上课看漫画的检讨书
2014/09/26 职场文书
毕业生代领毕业材料的授权委托书
2014/09/29 职场文书
单位领导婚礼致辞
2015/07/28 职场文书
撤回我也能看到!教你用Python制作微信防撤回脚本
2021/06/11 Python
css3中transform属性实现的4种功能
2021/08/07 HTML / CSS