【问题标题】:Printing text in color [duplicate]以彩色打印文本 [重复]
【发布时间】:2021-09-18 12:22:28
【问题描述】:

我正在尝试以不同的颜色打印文本。我已经安装了termcolor,但是当输入:

from termcolor import colored
print(colored("hello","blue"))

它出现了

[34mhello[0m .

【问题讨论】:

  • 我无法重现您的问题。您能否提供有关您的环境的更多详细信息:操作系统、运行此代码的位置以及 python 版本?
  • answer 特定于termcolor
  • 这个answer对于windows来说可能是一个更好的选择。

标签: python colors termcolor


【解决方案1】:

您可以在IPython 中使用此代码,例如Jupyter Notebookgoogle colab

!pip install termcolor
from termcolor import colored
print(colored("hello","blue"))

【讨论】:

    【解决方案2】:

    首先:pip install colorama
    还有秒:

    from colorama import Fore, Back, Style
    print(Fore.GREEN + Back.RED + Style.DIM + "hello world")
    

    (这是一个例子,整个pip包使用*描述click here)
    由于某些未知原因,当我打开 CMD 并尝试它不起作用但是当我在 VSCode 中编码并从那里以终端窗口作为 CMD 运行时它工作正常。

    【讨论】:

      猜你喜欢
      • 2012-09-28
      • 1970-01-01
      • 2023-04-06
      • 2021-05-06
      • 1970-01-01
      • 1970-01-01
      • 2016-03-16
      • 2011-05-03
      • 2021-05-12
      相关资源
      最近更新 更多