Python:字符串格式化程序以获得带下划线的标题 [英] Python: String formatter to get underlined headline

查看:22
本文介绍了Python:字符串格式化程序以获得带下划线的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了构建我的控制台输出,我想打印一些信息,我想从一个带下划线的标题开始.但是如何在不创建额外变量的情况下很好地做到这一点?

To structure my console output, I want to print some information and I would like to start with an underlined headline. But how to do it nicely without creating an extra variable?

现在我是这样做的:

print("{:s}
{:s}
".format("This is an underlined headline.", len("This is an underlined headline.") * "-"))

什么给了我想要的输出:

what gives me the desired output:

This is an underlined headline.
-------------------------------

但是那个代码很糟糕.有没有更好的格式字符串来实现这一点?

But that code is bad. Is there some better format string to achieve that?

print("{0:s}
?????
".format("This is an underlined headline.", "-"))

谢谢:)

推荐答案

也许可以尝试使用 ANSI 转义序列

class Format:
    end = '33[0m'
    underline = '33[4m'

print(Format.underline + 'Your text here' + Format.end)

它将打印出带下划线的文本,对于整个 ANSI 转义序列文档,请单击这里

It will print out underlined text, for the whole ANSI escape sequence documentation click here

这篇关于Python:字符串格式化程序以获得带下划线的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆