Python通过2种方法输出带颜色字体

作者:pfeiliu 时间:2023-02-08 20:53:22 

方法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种方法输出带颜色字体

颜色

字体色编号背景色编号颜色
3040黑色
3141红色
3242绿色
3343黄色
3444蓝色
3545紫色
3646青色
3747白色


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")

来源:https://www.cnblogs.com/pfeiliu/p/12003180.html

标签:python,输出,颜色,字体
0
投稿

猜你喜欢

  • Python面向对象中类(class)的简单理解与用法分析

    2021-01-05 00:07:37
  • 一个表单焦点效果函数

    2008-01-19 10:59:00
  • Python 装饰器代码解析

    2022-06-29 20:48:01
  • Vue组件全局注册实现警告框的实例详解

    2024-05-02 16:53:05
  • 一篇文章带你了解python标准库--os模块

    2023-06-23 01:19:10
  • python字典操作实例详解

    2021-05-21 08:22:24
  • .Net行为型设计模式之观察者模式(Observer)

    2024-05-13 09:17:50
  • MySQL中的随机抽取的实现

    2024-01-17 21:07:42
  • vue中渐进过渡效果实现

    2024-04-10 10:32:54
  • 网页制作 JSP与ASP 的比较

    2005-08-10 16:00:00
  • CentOS 7安装MySQL的详细步骤

    2024-01-25 19:59:17
  • 使用组件来保护你的ASP代码

    2008-06-03 13:47:00
  • Python实现模拟登录及表单提交的方法

    2021-05-28 19:23:12
  • 基于OpenCV的PHP图像人脸识别技术

    2023-11-23 22:02:54
  • Linux下编译安装python3步骤

    2021-11-07 17:50:04
  • PHP程序员玩转Linux系列 nginx初学者引导

    2023-11-21 19:51:16
  • 用户 jb51net 登录失败。原因: 该帐户的密码必须更改

    2024-01-13 05:58:46
  • matplotlib给子图添加图例的方法

    2023-05-02 03:21:28
  • Task List 管理任务JavaScript源码

    2010-01-22 15:43:00
  • Python之多进程与多线程的使用

    2021-01-10 15:03:57
  • asp之家 网络编程 m.aspxhome.com