首页 技术 正文
技术 2022年11月21日
0 收藏 520 点赞 2,193 浏览 2208 个字

方法1:

(参考https://suixinblog.cn/2019/01/print-colorful.html)

使用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输出带颜色字体

颜色

字体色编号 背景色编号 颜色
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:

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模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,918
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,444
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,255
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,069
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,701
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,741