E.g:

print "hello"

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


当前回答

你可以使用termcolor来表示:

 sudo pip install termcolor

打印彩色粗体:

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

有关更多信息,请参阅PyPi上的termcolor。

Simple-colors是另一个具有类似语法的包:

 from simple_colors import *
 print(green('Hello', ['bold'])

在colorama中对应的可能是Style.BRIGHT。

其他回答

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

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

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

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

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

def say(text: str):  
  print ("\033[1;37m" + text)

say("Hello, world!")

我的代码工作正常。

有些终端允许打印彩色文本。有些颜色看起来就像“粗体”。试一试:

print ('\033[1;37mciao!')

序列'\033[1;37m'使一些终端以“亮白色”开始打印,可能看起来有点像粗体白色。'\033[0;0m'将关闭它。

这取决于你使用的是Linux还是Unix:

>>> start = "\033[1m"
>>> end = "\033[0;0m"
>>> print "The" + start + "text" + end + " is bold."
The text is bold.

“text”这个词应该加粗。