【问题标题】:Change The Colour Of Python Text更改 Python 文本的颜色
【发布时间】:2016-03-27 22:37:24
【问题描述】:

我正在尝试更改某行文本的颜色。 我看过其他文章(包括Change shell print color in python 3.3.2),但没有任何工作。 如果可能的话,我不想安装任何外部模块。

我的代码:

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.')

我在 Linux 上使用 Python 3.2.3。 感谢您的任何回复。

【问题讨论】:

标签: python colors passwords


【解决方案1】:

我认为你使用的方式是针对 Windows 的。

你可以使用termcolor:

from termcolor import colored

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

您也可以使用here 列出的颜色格式代码,像这样使用它们:

>>> 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.

输出如下:

【讨论】:

  • 不起作用,但还是谢谢。我仍然会将其标记为最佳答案。
  • 您必须先安装 termcolor:在 Windows Powershell pip install termcolor 或 Mac 终端:pip3 install termcolor
猜你喜欢
  • 2012-06-18
  • 1970-01-01
  • 2023-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-04
  • 2012-02-17
相关资源
最近更新 更多