首页 > python教程

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

时间:2020-08-09 python教程 查看: 592

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

<p style="text-align: center"><img alt="" src="https://img.94e.cn/media/uploads/full/20200809/202003021405526.png"></p>
<p>颜色</p>
<p>
</p><table>
  <thead>
    <tr>
      <th>字体色编号</th>
      <th>背景色编号</th>
      <th>颜色</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>30</td>
      <td>40</td>
      <td>黑色</td>
    </tr>
    <tr>
      <td>31</td>
      <td>41</td>
      <td>红色</td>
    </tr>
    <tr>
      <td>32</td>
      <td>42</td>
      <td>绿色</td>
    </tr>
    <tr>
      <td>33</td>
      <td>43</td>
      <td>黄色</td>
    </tr>
    <tr>
      <td>34</td>
      <td>44</td>
      <td>蓝色</td>
    </tr>
    <tr>
      <td>35</td>
      <td>45</td>
      <td>紫色</td>
    </tr>
    <tr>
      <td>36</td>
      <td>46</td>
      <td>青色</td>
    </tr>
    <tr>
      <td>37</td>
      <td>47</td>
      <td>白色</td>
    </tr>
  </tbody>
</table>


```python  
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博客。

展开全文
上一篇:python关于变量名的基础知识点
下一篇:Python生成六万个随机,唯一的8位数字和数字组成的随机字符串实例
输入字:
相关知识
Python 实现图片色彩转换案例

我们在看动漫、影视作品中,当人物在回忆过程中,体现出来的画面一般都是黑白或者褐色的。本文将提供将图片色彩转为黑白或者褐色风格的案例详解,感兴趣的小伙伴可以了解一下。

python初学定义函数

这篇文章主要为大家介绍了python的定义函数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助,希望能够给你带来帮助

图文详解Python如何导入自己编写的py文件

有时候自己写了一个py文件,想要把它导入到另一个py文件里面,所以下面这篇文章主要给大家介绍了关于Python如何导入自己编写的py文件的相关资料,需要的朋友可以参考下

python二分法查找实例代码

二分算法是一种效率比较高的查找算法,其输入的是一个有序的元素列表,如果查找元素包含在列表中,二分查找返回其位置,否则返回NONE,下面这篇文章主要给大家介绍了关于python二分法查找的相关资料,需要的朋友可以参考下