简体   繁体   English

更改Python文本的颜色

[英]Change The Colour Of Python Text

I am trying to change the colour of a certain line of text. 我正在尝试更改某行文本的颜色。 I have looked at other articles (including Change shell print color in python 3.3.2 ) but none of the work. 我看过其他文章(包括python 3.3.2中的Change shell print color ),但没有任何工作。 I would not like to install any external modules if that is possible. 如果可能,我不想安装任何外部模块。

My code: 我的代码:

from subprocess import call
call('color a', shell=True) #this sets the color to light green
print('The quick brown fox jumps over the lazy dog.')

I am using Python 3.2.3 on Linux. 我在Linux上使用Python 3.2.3。 Thanks for any responses. 感谢您的任何答复。

I think the way you are using is aimed at Windows. 我认为您使用的方式针对Windows。

You may use termcolor : 您可以使用termcolor

from termcolor import colored

print colored('hello', 'red'), colored('world', 'green')

You may use also color formatting codes listed here , use them like that: 您也可以使用此处列出的颜色格式代码,像这样使用它们:

>>> text = "The quick brown fox jumps over the lazy dog."
>>> print('\033[1;32m'+text+'\033[1;m')
The quick brown fox jumps over the lazy dog.

Output would be like: 输出如下:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM