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

颜色

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

方法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输出带颜色字体的更多相关文章

  1. python输出带颜色字体详解

    在python开发的过程中,经常会遇到需要打印各种信息.海量的信息堆砌在控制台中,就会导致信息都混在一起,降低了重要信息的可读性.这时候,如果能给重要的信息加上字体颜色,那么就会更加方便用户阅读了. ...

  2. (转)shell脚本输出带颜色字体

    shell脚本输出带颜色字体 原文:http://blog.csdn.net/andylauren/article/details/60873400 输出特效格式控制:\033[0m  关闭所有属性  ...

  3. Python3使用Print输出带颜色字体

    Phton3使用print输出带颜色的彩色字体 实现过程:       终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关.       转义序列是以ESC开头,即用\03 ...

  4. SHELL输出带颜色字体

    输出特效格式控制:\033[0m  关闭所有属性  \033[1m   设置高亮度  \03[4m   下划线  \033[5m   闪烁  \033[7m   反显  \033[8m   消隐  \ ...

  5. shell脚本输出带颜色字体

    #!/bin/bash # #下面是字体输出颜色及终端格式控制 #字体色范围:30-37 echo -e "\033[30m 黑色字 \033[0m" echo -e " ...

  6. shell 输出带颜色字体

    输出特效格式控制:\033[0m  关闭所有属性  \033[1m   设置高亮度  \03[4m   下划线  \033[5m   闪烁  \033[7m   反显  \033[8m   消隐  \ ...

  7. Python 输出带颜色的文字方法

    输出文字带颜色 书写格式,和相关说明如下: #格式: 设置颜色:  \033[显示方式;前景色;背景色m     \033[0m 方法: 字体色 背景色 颜色 -------------------- ...

  8. python输出带颜色详解

    书写格式:     开头部分:\033[显示方式;前景色;背景色m + 结尾部分:\033[0m      注意:开头部分的三个参数:显示方式,前景色,背景色是可选参数,可以只写其中的某一个:另外由于 ...

  9. jenkins调用shell脚本 输出带颜色字体

    jenkins需要安装AnsiColor插件在构建环境项选择“color ansi console output” 安装插件AnsiColor shell 脚本相关颜色设置 echo -e " ...

随机推荐

  1. js中的方法如何传入多个参数

    js中的方法如何传入多个参数 $(function () { let parameter1 = 1; let parameter2 = 'Hello World'; let parameter3 = ...

  2. 【转】西门子PLC以太网 通讯协议 解析

    一直想把三菱和西门子这两个使用频率最高的PLC上位通讯,融合到WCS系统的框架里: 现在三菱主流使用Q系列,使用的是MC协议, 前一段时间也写过一个入门介绍: 三菱Q系列通讯方式设计说明 去年8月份, ...

  3. 使用 Spring Boot 构建 RESTful API

    1. 使用 Idea 创建 Spring Initializer 项目 在创建项目的对话框中添加 Web 和 Lombok,或者建立项目后在 pom.xml 中添加依赖: <dependency ...

  4. WPF 精修篇 属性动画

    原文:WPF 精修篇 属性动画 属性动画 是通过 Storyboard 来改变属性值 <Rectangle x:Name="rect" Width="200&quo ...

  5. 关于CSS特性和选择器

    1层叠  样式冲突的遵循就近原则, 2继承  子代会继承父代的某些属性,例如字体大小,字体颜色 3优先级 !important 权重最高 通配符 * 和 继承的属性 权重为 0 0 0 0 行内样式权 ...

  6. Python: 把txt文件转换成csv

    最近在项目上需要批量把txt文件转成成csv文件格式,以前是手动打开excel文件,然后导入txt来生产csv文件,由于这已经变成每周需要做的事情,决定用python自动化脚本来实现,思路: 读取文件 ...

  7. .Net IOC框架入门之——Autofac

    一.简介  Autofac是.NET领域最为流行的IOC框架之一,传说是速度最快的一个 目的 1.依赖注入的目的是为了解耦.2.不依赖于具体类,而依赖抽象类或者接口,这叫依赖倒置.3.控制反转即IoC ...

  8. A - A Compatible Pair-biaobiao88

    A - A Compatible Pair Nian is a monster which lives deep in the oceans. Once a year, it shows up on ...

  9. Spring Boot 实战 入门

    目前没有系统学习过 Spring 框架,参与工作时,直接参与到了 Spring Boot 项目的开发.目前还比较菜,所以,你要是和我一样,不妨也跳过 Spring 框架的学习,直接学习 Sring B ...

  10. 2019CISCN web题赛-JustSoSo;love_math(复现)

    0x00前言 这几天从网上找个CMS源码开始练习审计,盯着众多的代码debug调呀调头晕脑胀的,还不错找到个文件读取和一个ssrf... 上月底结束的CISCN线上赛,web四道,仔细研究的2道,做出 ...