from termcolor import colored, cprint # 使用colored函数 print(colored('hello, world!', 'red')) print(colored('green text', 'green')) print(colored('blue text', 'blue', attrs=['on_white'])) # 蓝色文字,白色背景 # 使用cprint函数 cprint('hello, world!', 'green', 'on_red') # ...
「安装:」pip install termcolorcprint() 功能相当于print(colored(something))。cprint('sometext','color','on_color',attrs=[])其中:color:文字颜色。可选,grey,red,green,yellow,blue,magenta,cyan,white。on_color:背景色,可选,on_grey,on_red,on_green,on_yellow,on_blue,on_magenta...
print(Style.RESET_ALL) print('Back to normal text') 效果: 方法三 需要termcolor: pip install termcolor from termcolor import colored print(colored('Hello, World!', 'red')) print(colored('Green text', 'green')) print(colored('Blue text', 'blue', 'on_white')) # Blue text on white b...
defprint_colored(text, color_code):""" 参数1:要打印的文本 参数2:颜色代码(例如:\033[31m 为红色,\033[32m 为绿色) """print(f"{color_code}{text}\033[0m")# 示例用法red_color ="\033[31m"green_color ="\033[32m"print_colored("这是红色文字", red_color) print_colored("这是绿色文字...
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 backgroundimportsysfromtermcolorimportcolored, cprint ...
使用Colored.blue、Colored.yellow等成员函数,可以将传入的字符串文本加上转义序列输出,供下一步print。
"print(string) 函数封装 也可以通过函数进一步封装,让代码更加简洁。 defhighlight(string, fcolor='', bgcolor='', style=''):"""彩色打印的函数"""fcolor_code =getattr(Font, fcolor.upper(),'') bgcolor_code =getattr(Background, bgcolor.upper(),'')...
print(colored('红色文字', 'red') + colored('绿色文字', 'green') + colored('蓝色文字', 'blue')) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 通过上面的示例代码,我们可以看到如何在Python中使用`colorama`和`termcolor`库来实现文字多种颜色的效果。这些库都提...
colored_text=color_text("Hello, World!",Fore.RED) 1. 步骤4:输出彩色字体 最后一步是将彩色字体输出到屏幕上。使用print函数来实现: print(colored_text) 1. 3. 代码示例 下面是完整的代码示例: importcoloramafromcoloramaimportFore,Back,Styledefcolor_text(text,color):returncolor+text+Style.RESET_ALL#...
以下是一个简单的示例,展示了如何在print函数中使用颜色: def colored_print(text, color_code): print(f"\033[{color_code}m{text}\033[0m") # 使用不同的颜色代码 red_color_code = 31 green_color_code = 32 blue_color_code = 34 colored_print("红色文本", red_color_code) colored_print("...