Python通過(guò)2種方法輸出帶顏色字體
方法1:
使用Python中自帶的print輸出帶有顏色或者背景的字符串
書(shū)寫(xiě)語(yǔ)法
print(033[顯示方式;前景色;背景色m輸出內(nèi)容033[0m)
其中,顯示方式、前景色、背景色都是可選參數(shù)(可缺省一個(gè)或多個(gè))。
參數(shù)
顯示方式
顯示方式 效果 0 默認(rèn) 1 粗體 4 下劃線 5 閃爍 7 反白顯示
print('顯示方式:')print('033[0mSuixinBlog: https://suixinblog.cn033[0m')print('033[1mSuixinBlog: https://suixinblog.cn033[0m')print('033[4mSuixinBlog: https://suixinblog.cn033[0m')print('033[5mSuixinBlog: https://suixinblog.cn033[0m')print('033[7mSuixinBlog: https://suixinblog.cn033[0m')
顏色
字體色編號(hào) 背景色編號(hào) 顏色 30 40 黑色 31 41 紅色 32 42 綠色 33 43 黃色 34 44 藍(lán)色 35 45 紫色 36 46 青色 37 47 白色
print('字體色:')print('033[30mSuixinBlog: https://suixinblog.cn033[0m')print('033[31mSuixinBlog: https://suixinblog.cn033[0m')print('033[32mSuixinBlog: https://suixinblog.cn033[0m')print('033[4;33mSuixinBlog: https://suixinblog.cn033[0m')print('033[34mSuixinBlog: https://suixinblog.cn033[0m')print('033[1;35mSuixinBlog: https://suixinblog.cn033[0m')print('033[4;36mSuixinBlog: https://suixinblog.cn033[0m')print('033[37mSuixinBlog: https://suixinblog.cn033[0m')print('背景色:')print('033[1;37;40mtSuixinBlog: https://suixinblog.cn033[0m')print('033[37;41mtSuixinBlog: https://suixinblog.cn033[0m')print('033[37;42mtSuixinBlog: https://suixinblog.cn033[0m')print('033[37;43mtSuixinBlog: https://suixinblog.cn033[0m')print('033[37;44mtSuixinBlog: https://suixinblog.cn033[0m')print('033[37;45mtSuixinBlog: https://suixinblog.cn033[0m')print('033[37;46mtSuixinBlog: https://suixinblog.cn033[0m')print('033[1;30;47mtSuixinBlog: https://suixinblog.cn033[0m')
方法2:
colorama是一個(gè)python專門用來(lái)在控制臺(tái)、命令行輸出彩色文字的模塊,可以跨平臺(tái)使用。
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
跨平臺(tái)印刷彩色文本可以使用彩色光的常數(shù)簡(jiǎn)稱ANSI轉(zhuǎn)義序列:
from colorama import Fore,Back,Styleprint (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關(guān)鍵字參數(shù):
init()接受一些* * kwargs覆蓋缺省行為
init(autoreset = False):
如果你發(fā)現(xiàn)自己一再發(fā)送重置序列結(jié)束時(shí)關(guān)閉顏色變化每一個(gè)打印,然后init(autoreset = True)將自動(dòng)化示例:
from colorama import init,Foreinit(autoreset=True)print (Fore.RED + 'welcome to python !!')print ('automatically back to default color again')
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Xml簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理2. asp使用Weekday函數(shù)計(jì)算項(xiàng)目的結(jié)束時(shí)間3. 告別AJAX實(shí)現(xiàn)無(wú)刷新提交表單4. XML入門的常見(jiàn)問(wèn)題(一)5. asp讀取xml文件和記數(shù)6. webpack高級(jí)配置與優(yōu)化詳解7. 讀寫(xiě)xml文件的2個(gè)小函數(shù)8. 不同瀏覽器對(duì)XML的解析是不同的9. IE6/IE7/IE8/IE9中tbody的innerHTML不能賦值的完美解決方案10. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案
