python print 打印彩色字体

全局说明

演示了如何使用ANSI转义序列打印不同颜色的字体


一、颜色说明

\033[ :开始颜色设置。
1 :字体加粗。
32 :文字颜色,绿色。
41 :背景颜色,红色。
m :结束颜色设置。
\033[0m :重置所有终端属性,包括颜色。

image


二、输出示例

# 打印红色字体
print("\033[1;31;40m这是红色的字体\033[0m")

# 打印绿色字体
print("\033[1;32;40m这是绿色的字体\033[0m")

# 打印黄色字体
print("\033[1;33;40m这是黄色的字体\033[0m")

# 打印蓝色字体
print("\033[1;34;40m这是蓝色的字体\033[0m")

# 打印紫色字体
print("\033[1;35;40m这是紫色的字体\033[0m")

# 打印青色字体
print("\033[1;36;40m这是青色的字体\033[0m")

# 打印白色字体
print("\033[1;37;40m这是白色的字体\033[0m")

image


二、颜色设置成变量,调用更方便

## 彩色提醒字体
color_err = "[\033[1;31m  Error \033[0m]"  ## 红色 Error
color_ok = "[\033[1;32m   OK   \033[0m]"  ## 绿色 ok
color_war = "[\033[1;33m Warning \033[0m]"  ## 黄色 Warning
color_cmd = "[\033[1;34m Command \033[0m]"  ## 蓝色 Command
color_info = "[\033[1;36m  Info  \033[0m]"  ## 青蓝色 Info

user_file="text.txt"
print(f"\n{color_err} 请检查 %s 文件是否存在 或 拼写是否正确!!!\n"  % user_file )
print(f"\n{color_ok} 请检查 %s 文件是否存在 或 拼写是否正确!!!\n"  % user_file )
print(f"\n{color_war} 请检查 %s 文件是否存在 或 拼写是否正确!!!\n"  % user_file )
print(f"\n{color_cmd} 请检查 %s 文件是否存在 或 拼写是否正确!!!\n"  % user_file )
print(f"\n{color_info} 请检查 %s 文件是否存在 或 拼写是否正确!!!\n"  % user_file )

image


免责声明:本号所涉及内容仅供安全研究与教学使用,如出现其他风险,后果自负。




参考、来源:
https://baijiahao.baidu.com/s?id=1775801150627519610&wfr=spider&for=pc
https://blog.51cto.com/u_16213440/7503081



posted @ 2023-11-06 09:55  悟透  阅读(874)  评论(0编辑  收藏  举报