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

查看:495
本文介绍了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}\n{:s}\n".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}\n?????\n".format("This is an underlined headline.", "-"))

谢谢:)

推荐答案

也许尝试使用 ANSI转义序列

class Format:
    end = '\033[0m'
    underline = '\033[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天全站免登陆