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操作MySQL数据库的方法
Jun 20 Python
详解Django+Uwsgi+Nginx的生产环境部署
Jun 25 Python
python selenium登录豆瓣网过程解析
Aug 10 Python
利用Python产生加密表和解密表的实现方法
Oct 15 Python
Pandas时间序列重采样(resample)方法中closed、label的作用详解
Dec 10 Python
Matplotlib使用Cursor实现UI定位的示例代码
Mar 12 Python
pandas apply多线程实现代码
Aug 17 Python
python时间time模块处理大全
Oct 25 Python
Django xadmin安装及使用详解
Oct 26 Python
python对输出的奇数偶数排序实例代码
Dec 04 Python
Django与AJAX实现网页动态数据显示的示例代码
Feb 24 Python
68行Python代码实现带难度升级的贪吃蛇
Jan 18 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
PHP及Zend Engine的线程安全模型分析
2011/11/10 PHP
解析PHP中的unset究竟会不会释放内存
2013/07/18 PHP
php如何获取文件的扩展名
2015/10/28 PHP
PHP异常处理Exception类
2015/12/11 PHP
php注册和登录界面的实现案例(推荐)
2016/10/24 PHP
基于php实现的验证码小程序
2016/12/13 PHP
PHP基于接口技术实现简单的多态应用完整实例
2017/04/26 PHP
PHP PDO和消息队列的个人理解与应用实例分析
2019/11/25 PHP
JavaScript学习笔记(十)
2010/01/17 Javascript
解决jQuery插件tipswindown与hintbox冲突
2010/11/05 Javascript
js制作简易年历完整实例
2015/01/28 Javascript
JavaScript实现DIV层拖动及动态增加新层的方法
2015/05/12 Javascript
使用jQuery Rotare实现微信大转盘抽奖功能
2016/06/20 Javascript
浅析JavaScript中命名空间namespace模式
2016/06/22 Javascript
解决node.js安装包失败的几种方法
2016/09/02 Javascript
浅谈Angular路由守卫
2017/08/26 Javascript
详解jQuery中的prop()使用方法
2020/01/05 jQuery
nodejs nedb 封装库与使用方法示例
2020/02/06 NodeJs
sharp.js安装过程中遇到的问题总结
2020/04/02 Javascript
python对象及面向对象技术详解
2016/07/19 Python
Python基于Flask框架配置依赖包信息的项目迁移部署
2018/03/02 Python
python Django中models进行模糊查询的示例
2019/07/18 Python
Python标准库json模块和pickle模块使用详解
2020/03/10 Python
使用CSS3制作一个简单的Chrome模拟器
2015/07/15 HTML / CSS
html5文本内容_动力节点Java学院整理
2017/07/11 HTML / CSS
伦敦最有品味的百货:Liberty London
2016/11/12 全球购物
Farfetch香港官网:汇集全球时尚奢侈品购物平台
2017/11/26 全球购物
专科毕业生就业推荐信
2013/11/01 职场文书
会计专业个人求职信范文
2014/01/08 职场文书
八年级物理教学反思
2014/01/19 职场文书
库房管理员岗位职责
2014/03/09 职场文书
制药工程专业职业生涯规划范文
2014/03/10 职场文书
副总经理党的群众路线教育实践活动个人对照检查材料思想汇报
2014/10/06 职场文书
2014感恩节演讲稿大全
2014/10/11 职场文书
Python爬虫之自动爬取某车之家各车销售数据
2021/06/02 Python