E.g:

print "hello"

我该怎么做才能将“hello”文本加粗?


当前回答

粗体文本在Python中是这样的:

print("This is how the {}bold{} text looks like in Python".format('\033[1m', '\033[0m'))

这就是Python中粗体文本的样子。

其他回答

class color:
   PURPLE = '\033[95m'
   CYAN = '\033[96m'
   DARKCYAN = '\033[36m'
   BLUE = '\033[94m'
   GREEN = '\033[92m'
   YELLOW = '\033[93m'
   RED = '\033[91m'
   BOLD = '\033[1m'
   UNDERLINE = '\033[4m'
   END = '\033[0m'

print(color.BOLD + 'Hello, World!' + color.END)
def say(text: str):  
  print ("\033[1;37m" + text)

say("Hello, world!")

我的代码工作正常。

假设你指的是真正的打印终端上的“打印”:

>>> text = 'foo bar\r\noof\trab\r\n'
>>> ''.join(s if i & 1 else (s + '\b' * len(s)) * 2 + s
...         for i, s in enumerate(re.split(r'(\s+)', text)))
'foo\x08\x08\x08foo\x08\x08\x08foo bar\x08\x08\x08bar\x08\x08\x08bar\r\noof\x08\
x08\x08oof\x08\x08\x08oof\trab\x08\x08\x08rab\x08\x08\x08rab\r\n'

把它发送到你的stdout。

安装termcolor模块

sudo pip install termcolor

然后对彩色文本试试这个

from termcolor import colored
print colored('Hello', 'green')

对于粗体文本:

from termcolor import colored
print colored('Hello', attrs=['bold'])

在Python 3中,除了常规的命名打印参数(如file或end)外,您还可以使用cprint作为内置打印的下拉替换,使用可选的第二个参数表示颜色或attrs参数表示粗体(以及其他属性,如下划线)。

import sys
from termcolor import cprint
cprint('Hello', 'green', attrs=['bold'], file=sys.stderr)

完全披露,这个答案很大程度上是基于奥卢·史密斯的答案 本来是想编辑一下,这样可以减少本页的噪音 相当多,但因为一些评论家的错误概念 编辑应该是什么样子,我现在不得不把这作为一个单独的答案。

print '\033[1m  Your Name  \033[0m'

\033[1m是终端中粗体的转义码。 \033[0m是结束编辑文本并返回默认文本格式的转义代码。

如果你不使用\033[0m],那么所有即将出现的终端文本都将变成粗体。