print("hello")
输出应该是单词 "hello",但要带有下划线。
你可以通过使用转义字符来实现。
print("\033[4mhello\033[0m")
print("\u0332".join("hello "))
我曾经遇到过这个问题,而且在我的情况下,问题出在了'colorama.init(autoreset=True)'这一行。由于某种原因,在Python3 Windows中,如果使用Colorama自动重置功能,则无法打印带有下划线的文本(您仍然可以打印加粗文本、前景色、背景色等,到目前为止,我发现只有下划线格式受到影响)。当然,这也意味着您需要通过添加ANSI重置序列ESC [0m
(在下面的示例中,这将是print语句末尾的\033[0m
)来手动重置文本。
import colorama
#colorama.init(autoreset=True) #This line was causing me problems
#import os #To use below line
#os.system("") #This line can make it work in some terminals
print("\033[1;4mBold and underlined text\033[0m")
经过进一步测试,以下是一些附加说明:
print语句
之前添加os.system("")
行可以使其正常工作。我在cmd上进行了测试并且它可以正常工作(colorama.init(autoreset=True)
行仍被注释掉)。编辑:为完整性添加了“import colorama”行和其他说明。
请确保您已经安装了Python 3.6+ 使用pip安装quo https://pypi.org/project/quo
from quo import echo
echo(f"Hello World!!!", underline=True)
确保您安装了Python3解释器 然后运行
x="the solution"
print("\033[4m" + x + "\033[0m")
string = 'Hello world'
emptystring = ''
for i in range(0, len(string)):
if string[i] == ' ':
emptystring = emptystring + string[i]
else:
emptystring= emptystring+string[i]+str('\u0332')
print(emptystring)