> For the complete documentation index, see [llms.txt](https://l1nwatch.gitbook.io/interview_exercise/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://l1nwatch.gitbook.io/interview_exercise/stackoverflow-about-python/zen-mo-zai-zhong-duan-li-shu-chu-yan-se.md).

# 怎么在终端里输出颜色?

## 方法一

[Python termcolor module 模块](https://pypi.python.org/pypi/termcolor)

```python
from termcolor import colored
print(colored("hello", "red"))
print(colored("world", "green"))
```

## 方法二

这依赖于你使用哪种操作系统，最常用的方法就是输出 ANSI 转义序列：

```python
class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
```

可以这么使用上面的代码：

```python
print(bcolors.WARNING + "Warning: No active frommets remain. Continue?" + bcolors.ENDC)
```

这种方法适合 macOS、Linux 和 Windows。还有一些其他的 ANSI 代码可以设置颜色，消除光标或者其他

如果想要应用更复杂的功能，应该看看 `curses` 模块，参考：[Python Curses HowTO](https://docs.python.org/2/howto/curses.html)

如果不使用拓展的 ASCII，比如不是一个 PC，那么 `#` 或者 `@` 可能是最好的选择

如果用的是 [IBM扩展ascii字符设置](http://telecom.tbi.net/asc-ibm.html)，你还可以有更多的选择。176,177,178和219是"块字符"。

一些现代的基于文本的程序，像 Swarf Fortress 显示的文本使用图像模式，而用的字体也是传统的计算机字体的图像。可以在 [Dwarf Fortress Wiki](http://dwarffortresswiki.org/DF2014:Tilesets) 找到你可以用的字符.

[Text Mode Demo Contest ](http://en.wikipedia.org/wiki/Text_Mode_Demo_Contest)也有许多资料可供参考。
