如何使用 Python 3 打印彩色输出? [英] How do I print colored output with Python 3?

查看:40
本文介绍了如何使用 Python 3 打印彩色输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的打印语句:

I have a simple print statement:

print('hello friends')

我希望终端中的输出为蓝色.我怎样才能用 Python3 做到这一点?

I would like the output to be blue in the terminal. How can I accomplish this with Python3?

推荐答案

colorama 非常简单a>,就这样做:

It is very simple with colorama, just do this:

import colorama
from colorama import Fore, Style
print(Fore.BLUE + "Hello World")

这是在Python3 REPL中的运行结果:

And here is the running result in Python3 REPL:

并调用它来重置颜色设置:

And call this to reset the color settings:

print(Style.RESET_ALL)

为了避免打印空行,请写下:

To avoid printing an empty line write this:

print(f"{Fore.BLUE}Hello World{Style.RESET_ALL}")

这篇关于如何使用 Python 3 打印彩色输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆