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计数排序和基数排序算法实例
Apr 25 Python
Python SQLAlchemy基本操作和常用技巧(包含大量实例,非常好)
May 06 Python
仅用500行Python代码实现一个英文解析器的教程
Apr 02 Python
python安装模块如何通过setup.py安装(超简单)
May 05 Python
Python中使用pypdf2合并、分割、加密pdf文件的代码详解
May 21 Python
python区块及区块链的开发详解
Jul 03 Python
简单了解python的内存管理机制
Jul 08 Python
python实现单目标、多目标、多尺度、自定义特征的KCF跟踪算法(实例代码)
Jan 08 Python
python GUI库图形界面开发之PyQt5美化窗体与控件(异形窗体)实例
Feb 25 Python
python传到前端的数据,双引号被转义的问题
Apr 03 Python
在 Windows 下搭建高效的 django 开发环境的详细教程
Jul 27 Python
分享3个非常实用的 Python 模块
Mar 03 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
建立动态的WML站点(二)
2006/10/09 PHP
如何利用PHP执行.SQL文件
2013/07/05 PHP
基于preg_match_all采集后数据处理的一点心得笔记(编码转换和正则匹配)
2014/01/31 PHP
PHP使用Redis替代文件存储Session的方法
2017/02/15 PHP
ZendFramework2连接数据库操作实例
2017/04/18 PHP
PHP7 echo和print语句实例用法
2019/02/15 PHP
javascript 正则替换 replace(regExp, function)用法
2010/05/22 Javascript
javascript实现检验的各种规则
2015/07/31 Javascript
JS+CSS实现的简单折叠展开多级菜单效果
2015/09/12 Javascript
基于javascript实现右下角浮动广告效果
2016/01/08 Javascript
AngularJS在IE下取数据总是缓存问题的解决方法
2016/08/05 Javascript
手机端实现Bootstrap简单图片轮播效果
2016/10/13 Javascript
jquery广告无缝轮播实例
2017/01/05 Javascript
javascript循环链表之约瑟夫环的实现方法
2017/01/16 Javascript
setTimeout函数的神奇使用
2017/02/26 Javascript
ES6数组的扩展详解
2017/04/25 Javascript
Vue.js教程之axios与网络传输的学习实践
2017/04/29 Javascript
ES6下React组件的写法示例代码
2017/05/04 Javascript
微信JSAPI Ticket接口签名详解
2020/06/28 Javascript
JS设计模式之命令模式概念与用法分析
2018/02/06 Javascript
JavaScript实现美化滑块效果
2019/05/17 Javascript
Python日期的加减等操作的示例
2017/08/15 Python
基于python 字符编码的理解
2017/09/02 Python
python实现图片转字符小工具
2019/04/30 Python
pandas 强制类型转换 df.astype实例
2020/04/09 Python
如何使用Python处理HDF格式数据及可视化问题
2020/06/24 Python
Python连接mysql数据库及简单增删改查操作示例代码
2020/08/03 Python
基于pycharm 项目和项目文件命名规则的介绍
2021/01/15 Python
HTML5播放实现rtmp流直播
2020/06/16 HTML / CSS
Belvilla法国:休闲度假房屋出租
2020/10/03 全球购物
波兰汽车配件网上商店:iParts.pl
2020/09/08 全球购物
医药大学生求职简历的自我评价
2013/10/17 职场文书
实用的简历自我评价
2014/03/06 职场文书
大学生求职计划书
2014/04/30 职场文书
2015年学生会干事工作总结
2015/04/09 职场文书
Python基础学习之奇异的GUI对话框
2021/05/27 Python