E.g:

print "hello"

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


当前回答

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

from colorama import *

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

其他回答

Python中有一个非常有用的模块用于格式化文本(粗体、下划线、颜色等)。它使用curses库,但是使用起来非常简单。

一个例子:

from terminal import render
print render('%(BG_YELLOW)s%(RED)s%(BOLD)sHey this is a test%(NORMAL)s')
print render('%(BG_GREEN)s%(RED)s%(UNDERLINE)sAnother test%(NORMAL)s')

我写了一个名为colors.py的简单模块,让它更python化一些:

import colors

with colors.pretty_output(colors.BOLD, colors.FG_RED) as out:
    out.write("This is a bold red text")

with colors.pretty_output(colors.BG_GREEN) as out:
    out.write("This output have a green background but you " +
               colors.BOLD + colors.FG_RED + "can" + colors.END + " mix styles")

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

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

“text”这个词应该加粗。

粗体文本在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)

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

from colorama import *

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