日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

在python终端中打印颜色的3中方式(python3经典编程案例)

發布時間:2024/1/8 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在python终端中打印颜色的3中方式(python3经典编程案例) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在 Python 中有幾種方法可以將彩色文本輸出到終端。 最常見的做法是:

1、使用內置模塊:colorama 模塊

可以使用 Colorama 的 ANSI 轉義序列的常量簡寫來完成彩色文本的跨平臺打印:
案例1:

from colorama import Fore, Back, Styleprint(Fore.RED + 'some red text') print(Fore.YELLOW + 'some red text') print(Fore.BLUE + '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')

案例二:

# colorama是一個python專門用來在控制臺、命令行輸出彩色文字的模塊,可以跨平臺使用。 # 安裝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 from colorama import Fore,Back,Style print (Fore.RED + "some red text") print (Fore.GREEN + "some red text") print (Fore.YELLOW + "some red text") print (Fore.BLUE + "some red text") print (Fore.MAGENTA + "some red text") print (Fore.CYAN + "some red text") print (Fore.RESET + "some red text") print (Fore.WHITE + "some red text") print (Fore.BLACK + "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")

2、使用termcolor模塊:

termcolor 是一個 Python 模塊,用于在終端中輸出 ANSII 顏色格式。

# Python program to print # colored text and background import sys from termcolor import colored, cprinttext = colored('Hello, World!', 'red', attrs=['reverse', 'blink']) print(text) cprint('Hello, World!', 'green', 'on_red')print_red_on_cyan = lambda x: cprint(x, 'red', 'on_cyan') print_red_on_cyan('Hello, World!') print_red_on_cyan('Hello, Universe!')for i in range(10):cprint(i, 'magenta', end=' ')cprint("Attention!", 'red', attrs=['bold'], file=sys.stderr)

3、使用 ANSI 轉義碼

打印彩色文本最常用的方法是直接打印 ANSI 轉義序列。 這可以以不同的格式交付,例如:

構建要調用的函數:我們可以構建函數來調用特定顏色命名的函數來執行相關的 ANSI 轉義序列。
案例一:

# 一. 使用Python中自帶的print輸出帶有顏色或者背景的字符串# 其中,顯示方式、前景色、背景色都是可選參數(可缺省一個或多個)。 print('\033[顯示方式;前景色;背景色m輸出內容\033[0m') print(f'\033[31m5. ---zhangjskf ---\033[0m ')print("顯示方式:") # 顯示方式 效果 # 0 默認 # 1 粗體 # 4 下劃線 # 5 閃爍 # 7 反白顯示 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")

案例二:

# Python program to print # colored text and background def prRed(skk): print("\033[91m {}\033[00m".format(skk))def prGreen(skk): print("\033[92m {}\033[00m".format(skk))def prYellow(skk): print("\033[93m {}\033[00m".format(skk))def prLightPurple(skk): print("\033[94m {}\033[00m".format(skk))def prPurple(skk): print("\033[95m {}\033[00m".format(skk))def prCyan(skk): print("\033[96m {}\033[00m".format(skk))def prLightGray(skk): print("\033[97m {}\033[00m".format(skk))def prBlack(skk): print("\033[98m {}\033[00m".format(skk))prCyan("Hello World, ") prYellow("It's") prGreen("Geeks") prRed("For") prGreen("Geeks")

總結

以上是生活随笔為你收集整理的在python终端中打印颜色的3中方式(python3经典编程案例)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。