E.g:

print "hello"

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


当前回答

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

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

其他回答

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)

用这个:

print '\033[1m' + 'Hello'

和变回正常:

print '\033[0m'

本页是打印颜色和字体粗细的很好的参考。转到“设置图形模式”部分:

注意,这并不适用于所有操作系统,但您不需要任何模块。

看看Colorama。它不一定对加粗有帮助……但是你可以在Windows和Linux上进行彩色输出,并控制亮度:

from colorama import *

init(autoreset=True)
print Fore.RED + 'some red text'
print Style.BRIGHT + Fore.RED + 'some bright red text'

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

>>> 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', attrs=['bold']))

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

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

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

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