如何在Python中打印粗体文本?

问题:如何在Python中打印粗体文本?

如何在Python中打印粗体文本?

例如:

print "hello"

我应该怎么做才能使文本“ hello”以粗体显示?

How do I print bold text in Python?

For example:

print "hello"

What should I do so that the text “hello” is displayed in bold?


回答 0

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

回答 1

用这个:

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

并恢复正常:

print '\033[0m'

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

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

Use this:

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

And to change back to normal:

print '\033[0m'

This page is a good reference for printing in colors and font-weights. Go to the section that says ‘Set graphics mode:’

And note this won’t work on all operating systems but you don’t need any modules.


回答 2

您可以为此使用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

You can use termcolor for this:

 sudo pip install termcolor

To print a colored bold:

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

For more information, see termcolor on PyPi.

simple-colors is another package with similar syntax:

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

The equivalent in colorama may be Style.BRIGHT.


回答 3

在简单的计算机编程中,没有诸如“打印粗体文本”之类的东西。让我们备份一下,了解您的文本是一串字节,而字节只是几捆。对于计算机,这是您的“ hello”文本,以二进制形式出现

0110100001100101011011000110110001101111

每个一个或零是一个位。每八个位是一个字节。像Python 2.x这样的字符串中的每个字节都是一个字母/数字/标点符号(称为字符)。因此,例如:

01101000 01100101 01101100 01101100 01101111
h        e        l        l        o

计算机将这些位转换为字母,但是在传统字符串(称为ASCII字符串)中,没有任何内容可以指示粗体文本。在工作原理稍有不同的Unicode字符串中,计算机可以支持国际语言字符(例如中文字符),但是同样,也不能说某些文本为粗体,而某些文本则不是。也没有明确的字体,文本大小等。

在打印HTML的情况下,您仍在输出字符串。但是,读取该字符串的计算机程序(Web浏览器)被编程为在将您的字母字符串转换为屏幕上的像素时将文本解释this is <b>bold</b>为“ this is bold ”。如果所有文本都是“所见即所得”,那么HTML本身的需求将得到缓解-您只需在编辑器中选择文本并将其加粗即可,而不用键入HTML。

其他程序使用不同的系统-很多答案解释了在终端上打印粗体文本的完全不同的系统。很高兴您发现了如何做自己想做的事情,但是在某个时候,您将想要了解字符串和内存的工作方式。

In straight-up computer programming, there is no such thing as “printing bold text”. Let’s back up a bit and understand that your text is a string of bytes and bytes are just bundles of bits. To the computer, here’s your “hello” text, in binary.

0110100001100101011011000110110001101111

Each one or zero is a bit. Every eight bits is a byte. Every byte is, in a string like that in Python 2.x, one letter/number/punctuation item (called a character). So for example:

01101000 01100101 01101100 01101100 01101111
h        e        l        l        o

The computer translates those bits into letters, but in a traditional string (called an ASCII string), there is nothing to indicate bold text. In a Unicode string, which works a little differently, the computer can support international language characters, like Chinese ones, but again, there’s nothing to say that some text is bold and some text is not. There’s also no explicit font, text size, etc.

In the case of printing HTML, you’re still outputting a string. But the computer program reading that string (a web browser) is programmed to interpret text like this is <b>bold</b> as “this is bold” when it converts your string of letters into pixels on the screen. If all text were WYSIWYG, the need for HTML itself would be mitigated — you would just select text in your editor and bold it instead of typing out the HTML.

Other programs use different systems — a lot of answers explained a completely different system for printing bold text on terminals. I’m glad you found out how to do what you want to do, but at some point, you’ll want to understand how strings and memory work.


回答 4

这取决于您使用的是linux / unix:

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

该词text应为粗体。

This depends if you’re using linux/unix:

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

The word text should be bold.


回答 5

退房colorama。它不一定有助于加粗…但是您可以在Windows和Linux上进行彩色输出,并控制亮度:

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

Check out colorama. It doesn’t necessarily help with bolding… but you can do colorized output on both Windows and Linux, and control the brightness:

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

回答 6

有一个非常有用的模块,用于在Python中设置文本格式(粗体,下划线,颜色..)。它使用curseslib,但是使用起来非常简单。

一个例子:

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的简单模块,以使其变得更加Pythonic:

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")

There is a very useful module for formatting text (bold, underline, colors..) in Python. It uses curses lib but it’s very straight-forward to use.

An example:

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')

UPDATED:

I wrote a simple module named colors.py to make this a little more pythonic:

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")

回答 7

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

\ 033 [1m是终端中加粗的Unicode \ 033 [0m是结束已编辑文本并返回默认文本格式的unicode !!!!

如果您不使用\ 033 [0m,则终端的所有后续文本将变为粗体!!!!!!

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

\033[1m is the unicode for bold in the terminal \033[0m is the unicode for end the edited text and back default text formate!!!!!

if you do not use \033[0m than all upcoming text of the terminal will become bold!!!!!!!!!


回答 8

安装termcolor模块

sudo pip install termcolor

然后尝试彩色文本

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

或以下粗体字:

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

在Python 3 cprintprint,除了常规的命名参数(或)之外,您还可以使用可选的第二个颜色attrs参数或用于加粗的参数(以及其他属性,如underline)来代替内置print函数。fileend

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

完全公开,此答案很大程度上基于Olu Smith的答案 ,并打算作为编辑,它会大大减少此页面上的干扰,但是由于某些评论者对编辑内容的误解,我现在被迫将其作为一个单独的答案。

Install the termcolor module

sudo pip install termcolor

and then try this for colored text

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

or this for bold text:

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

In Python 3 you can alternatively use cprint as a drop-in replacement for the built-in print, with the optional second parameter for colors or the attrs parameter for bold (and other attributes such as underline) in addition to the normal named print arguments such as file or end.

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

Full disclosure, this answer is heavily based on Olu Smith’s answer and was intended as an edit, which would have reduced the noise on this page considerably but because of some reviewers’ misguided concept of what an edit is supposed to be, I am now forced to make this a separate answer.


回答 9

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

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

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

Some terminals allow to print colored text. Some colors look like if they are “bold”. Try:

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

The sequence ‘\033[1;37m’ makes some terminals to start printing in “bright white” that may look a bit like bolded white. ‘\033[0;0m’ will turn it off.


回答 10

假设你真的一上就可以“打印” 真正的打印终端:

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

Assuming that you really mean “print” on a real printing terminal:

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

Just send that to your stdout.


回答 11

简单大胆-两行代码

在python 3中,您可以使用colorama-simple_colors:(“简单颜色”页面:https : //pypi.org/project/simple-colors/-转到标题“用法”。)在执行以下操作之前,请确保您pip install simple_colours

from simple_colors import *
print(green('hello', 'bold'))

Simple Boldness – Two Line Code

In python 3 you could use colorama – simple_colors: (Simple Colours page: https://pypi.org/project/simple-colors/ – go to the heading ‘Usage’.) Before you do what is below, make sure you pip install simple_colours.

from simple_colors import *
print(green('hello', 'bold'))