Python通過2種方法輸出帶顏色字體
方法1:
使用Python中自帶的print輸出帶有顏色或者背景的字符串
書寫語法
print(\033[顯示方式;前景色;背景色m輸出內(nèi)容\033[0m)
其中,顯示方式、前景色、背景色都是可選參數(shù)(可缺省一個或多個)。
參數(shù)
顯示方式
| 顯示方式 | 效果 |
|---|---|
| 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
可用格式常數(shù):
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
跨平臺印刷彩色文本可以使用彩色光的常數(shù)簡稱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關鍵字參數(shù):
init()接受一些* * kwargs覆蓋缺省行為
init(autoreset = False):
如果你發(fā)現(xiàn)自己一再發(fā)送重置序列結束時關閉顏色變化每一個打印,然后init(autoreset = True)將自動化
示例:
from colorama import init,Fore
init(autoreset=True)
print (Fore.RED + "welcome to python !!")
print ("automatically back to default color again")
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python中selenium_webdriver下拉框操作指南
selenium 雖然過了這么多年,但是到目前為止依然是比較流行的自動化框架了,下面這篇文章主要給大家介紹了關于Python中selenium_webdriver下拉框操作的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-01-01
Pandas 缺失數(shù)據(jù)處理的實現(xiàn)
這篇文章主要介紹了Pandas 缺失數(shù)據(jù)處理的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11
python中實現(xiàn)將多個print輸出合成一個數(shù)組
下面小編就為大家分享一篇python中實現(xiàn)將多個print輸出合成一個數(shù)組,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
Python實現(xiàn)HTTP協(xié)議下的文件下載方法總結
這篇文章主要介紹了Python實現(xiàn)HTTP協(xié)議下的文件下載方法總結,包括端點續(xù)傳下載等功能,需要的朋友可以參考下2016-04-04
Python爬蟲實例_城市公交網(wǎng)絡站點數(shù)據(jù)的爬取方法
下面小編就為大家分享一篇Python爬蟲實例_城市公交網(wǎng)絡站點數(shù)據(jù)的爬取方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
python創(chuàng)建關聯(lián)數(shù)組(字典)的方法
這篇文章主要介紹了python創(chuàng)建關聯(lián)數(shù)組(字典)的方法,涉及Python操作字典的相關技巧,非常具有實用價值,需要的朋友可以參考下2015-05-05

