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 04 Python
python实现按行切分文本文件的方法
Apr 18 Python
Pyhton中单行和多行注释的使用方法及规范
Oct 11 Python
python爬虫入门教程--优雅的HTTP库requests(二)
May 25 Python
特征脸(Eigenface)理论基础之PCA主成分分析法
Mar 13 Python
python 自动批量打开网页的示例
Feb 21 Python
python实现Dijkstra算法的最短路径问题
Jun 21 Python
python selenium爬取斗鱼所有直播房间信息过程详解
Aug 09 Python
python yield和Generator函数用法详解
Feb 10 Python
Python sqlite3查询操作过程解析
Feb 20 Python
python爬虫请求头的使用
Dec 01 Python
python 基于opencv 实现一个鼠标绘图小程序
Dec 11 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操作excel文件 基于phpexcel
2010/07/02 PHP
浅谈php优化需要注意的地方
2014/11/27 PHP
php字符串分割函数用法实例
2015/03/17 PHP
PHP实现权限管理功能示例
2017/09/22 PHP
yii 框架实现按天,月,年,自定义时间段统计数据的方法分析
2020/04/04 PHP
ThinkPHP5分页paginate代码实例解析
2020/11/10 PHP
JQuery扩展插件Validate—6 radio、checkbox、select的验证
2011/09/05 Javascript
dwz 如何去掉ajaxloading具体代码
2013/05/22 Javascript
JS中操作JSON总结
2020/12/06 Javascript
基于promise.js实现nodejs的promises库
2014/07/06 NodeJs
Mvc提交表单的四种方法全程详解
2016/08/10 Javascript
浅谈JavaScript事件绑定的常用方法及其优缺点分析
2016/11/01 Javascript
jQuery实现6位数字密码输入框
2016/12/29 Javascript
微信小程序 video详解及简单实例
2017/01/16 Javascript
Node.js操作redis实现添加查询功能
2017/05/25 Javascript
从零开始实现Vue简单的Toast插件
2018/12/03 Javascript
Bootstrap Paginator+PageHelper实现分页效果
2018/12/29 Javascript
js数组中去除重复值的几种方法
2020/08/03 Javascript
wxPython事件驱动实例详解
2014/09/28 Python
MySQL最常见的操作语句小结
2015/05/07 Python
python的unittest测试类代码实例
2017/12/07 Python
python使用正则表达式来获取文件名的前缀方法
2018/10/21 Python
python 基于TCP协议的套接字编程详解
2019/06/29 Python
django之状态保持-使用redis存储session的例子
2019/07/28 Python
10分钟教你用python动画演示深度优先算法搜寻逃出迷宫的路径
2019/08/12 Python
Python socket实现的文件下载器功能示例
2019/11/15 Python
使用pandas 将DataFrame转化成dict
2019/12/10 Python
Python模块 _winreg操作注册表
2020/02/05 Python
Python偏函数Partial function使用方法实例详解
2020/06/17 Python
python openCV实现摄像头获取人脸图片
2020/08/20 Python
手把手教你从PyCharm安装到激活(最新激活码),亲测有效可激活至2089年
2020/11/25 Python
大学生求职自荐信
2013/12/12 职场文书
法学函授自我鉴定
2014/02/06 职场文书
2014年财政工作总结
2014/12/10 职场文书
运动会通讯稿600字
2015/07/20 职场文书
MySQL和Oracle批量插入SQL的通用写法示例
2021/11/17 MySQL