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 相关文章推荐
用pywin32实现windows模拟鼠标及键盘动作
Apr 22 Python
python单元测试unittest实例详解
May 11 Python
Python爬虫爬取一个网页上的图片地址实例代码
Jan 16 Python
用Python shell简化开发
Aug 08 Python
python实现zabbix发送短信脚本
Sep 17 Python
Windows 安装 Anaconda3+PyCharm的方法步骤
Jun 13 Python
python基于gevent实现并发下载器代码实例
Nov 01 Python
Python调用scp向服务器上传文件示例
Dec 22 Python
Python使用正则实现计算字符串算式
Dec 29 Python
Django配置Bootstrap, js实现过程详解
Oct 13 Python
python 实现aes256加密
Nov 27 Python
人工智能深度学习OpenAI baselines的使用方法
May 20 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处理文件的小例子(解压缩,删除目录)
2013/02/03 PHP
深入讲解PHP的Yii框架中的属性(Property)
2016/03/18 PHP
PHP程序中的文件锁、互斥锁、读写锁使用技巧解析
2016/03/21 PHP
PHP图片加水印实现方法
2016/05/06 PHP
thinkPHP5.0框架环境变量配置方法
2017/03/17 PHP
微信第三方登录(原生)demo【必看篇】
2017/05/26 PHP
JavaScript 继承的实现
2009/07/09 Javascript
用apply让javascript函数仅执行一次的代码
2010/06/27 Javascript
网络之美 JavaScript中Get和Set访问器的实现代码
2010/09/19 Javascript
jQuery表格排序组件-tablesorter使用示例
2014/05/26 Javascript
AngularJS语法详解(续)
2015/01/23 Javascript
jQuery实现图片上传和裁剪插件Croppie
2015/11/29 Javascript
Javascript表单特效之十大常用原理性样例代码大总结
2016/07/12 Javascript
js实现页面刷新滚动条位置不变
2016/11/27 Javascript
JS实现类似百叶窗下拉菜单效果
2016/12/30 Javascript
js获取当前页的URL与window.location.href简单方法
2017/02/13 Javascript
详解vue项目构建与实战
2017/06/27 Javascript
js实现贪吃蛇小游戏(加墙)
2020/07/31 Javascript
从表单校验看JavaScript策略模式的使用详解
2020/10/17 Javascript
[01:59]DOTA2首部纪录片《Free to play》预告片
2014/03/12 DOTA
使用Python导出Excel图表以及导出为图片的方法
2015/11/07 Python
python自动循环定时开关机(非重启)测试
2019/08/26 Python
如何用OpenCV -python3实现视频物体追踪
2019/12/04 Python
Python decorator拦截器代码实例解析
2020/04/04 Python
python中pandas库中DataFrame对行和列的操作使用方法示例
2020/06/14 Python
python文件读取失败怎么处理
2020/06/23 Python
Ubuntu配置Pytorch on Graph (PoG)环境过程图解
2020/11/19 Python
英语专业个人求职信范文
2014/02/01 职场文书
文明演讲稿范文
2014/05/12 职场文书
2014村书记党建工作汇报材料
2014/11/02 职场文书
会计继续教育培训心得体会
2016/01/19 职场文书
pytorch交叉熵损失函数的weight参数的使用
2021/05/24 Python
还在手动盖楼抽奖?教你用Python实现自动评论盖楼抽奖(一)
2021/06/07 Python
SpringBoot集成Redis的思路详解
2021/10/16 Redis
java后台调用接口及处理跨域问题的解决
2022/03/24 Java/Android
Python使用pyecharts控件绘制图表
2022/06/05 Python