码迷,mamicode.com
首页 > 编程语言 > 详细

python print 输出带颜色字体

时间:2020-07-03 19:16:05      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:显示   过程   规范   html   asc   系统   com   部分   就是   

实现过程

终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关

转义序列是以ESC开头,即用\033来完成(ESC的ASCII码用十进制表示是27,用八进制表示就是033)

书写格式

开头部分:\033[显示方式;前景色;背景色m + 结尾部分:\033[0m

注意:开头部分的三个参数:显示方式,前景色,背景色是可选参数,可以只写其中的某一个;另外由于表示三个参数不同含义的数值都是唯一的没有重复的,所以三个参数的书写先后顺序没有固定要求,系统都能识别;但是,建议按照默认的格式规范书写

对于结尾部分,其实也可以省略,但是为了书写规范,建议\033[***开头,\033[0m结尾

数值表示的参数含义:

显示方式: 0(默认\)、1(高亮)、22(非粗体)、4(下划线)、24(非下划线)、 5(闪烁)、25(非闪烁)、7(反显)、27(非反显)

前景色: 30(黑色)、31(红色)、32(绿色)、 33(黄色)、34(蓝色)、35(洋 红)、36(青色)、37(白色)

背景色: 40(黑色)、41(红色)、42(绿色)、 43(黄色)、44(蓝色)、45(洋 红)、46(青色)、47(白色)

懒人代码示例(方便使用,即粘即用)

print("显示方式:")
print("\033[0;37;40m--- Replace the string ---\033[0m")
print("\033[1;37;40m--- Replace the string ---\033[0m")
print("\033[22;37;40m--- Replace the string ---\033[0m")
print("\033[4;37;40m--- Replace the string ---\033[0m")
print("\033[24;37;40m--- Replace the string ---\033[0m")
print("\033[5;37;40m--- Replace the string ---\033[0m")
print("\033[25;37;40m--- Replace the string ---\033[0m")
print("\033[7;37;40m--- Replace the string ---\033[0m")
print("\033[27;37;40m--- Replace the string ---\033[0m")
print("前景色:")
print("\033[0;30;40m--- Replace the string ---\033[0m")
print("\033[0;31;40m--- Replace the string ---\033[0m")
print("\033[0;32;40m--- Replace the string ---\033[0m")
print("\033[0;33;40m--- Replace the string ---\033[0m")
print("\033[0;34;40m--- Replace the string ---\033[0m")
print("\033[0;35;40m--- Replace the string ---\033[0m")
print("\033[0;36;40m--- Replace the string ---\033[0m")
print("\033[0;37;40m--- Replace the string ---\033[0m")
print("背景色:")
print("\033[0;37;40m--- Replace the string ---\033[0m")
print("\033[0;37;41m--- Replace the string ---\033[0m")
print("\033[0;37;42m--- Replace the string ---\033[0m")
print("\033[0;37;43m--- Replace the string ---\033[0m")
print("\033[0;37;44m--- Replace the string ---\033[0m")
print("\033[0;37;45m--- Replace the string ---\033[0m")
print("\033[0;37;46m--- Replace the string ---\033[0m")
print("\033[0;37;47m--- Replace the string ---\033[0m")

因为隔一段时间不用就忘,这样整一下子用起来方便。

此处为原博文传送门, 支持正版

python print 输出带颜色字体

标签:显示   过程   规范   html   asc   系统   com   部分   就是   

原文地址:https://www.cnblogs.com/shu-sheng/p/13232238.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!