E.g:
print "hello"
我该怎么做才能将“hello”文本加粗?
E.g:
print "hello"
我该怎么做才能将“hello”文本加粗?
当前回答
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")
其他回答
def say(text: str):
print ("\033[1;37m" + text)
say("Hello, world!")
我的代码工作正常。
这取决于你使用的是Linux还是Unix:
>>> start = "\033[1m"
>>> end = "\033[0;0m"
>>> print "The" + start + "text" + end + " is bold."
The text is bold.
“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。
简单的粗体-两行代码
在Python 3中,你可以使用Colorama - simple_colors: (在简单颜色页面*,转到标题“用法”。)在你做下面的事情之前。确保你安装了simple_colors。
from simple_colors import *
print(green('hello', 'bold'))
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")