我怎样才能使文字在Python中加粗? [英] How can I make text bold in Python?

查看:5581
本文介绍了我怎样才能使文字在Python中加粗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在Python中将文本更改为粗体。有没有办法做到这一点?



我已经能够用termcolor改变颜色,但没有用粗体文本。有没有人尝试过这样做? 如果您的平台可能要尝试ANSI转义序列允许它。



从相关的crossposting - 可能的重复 - SO问题: 1 2



在继续之前,请阅读上述链接中的警告信息,以确保您的操作系统和Python版本可以正确呈现。



您可以定义一个包含您所需的所有ANSI转义序列的类。

  class style:
BOLD ='\033 [1m'
END ='\033 [0m'



然后打印通过串联的类调用:

 打印样式.BOLD +这是我的文本字符串。+样式。 END 

如果您不想经历创建整个班级的麻烦,文本,你可以直接将它们直接连接起来。但是,如果你的代码是开源的,而不是个人使用,请确保你告诉其他潜在的读者这是什么!

 打印'\033 [1m'+'这是我的文本字符串'+'\033 [0m'

这是否回答您的问题?



编辑:JYelton击败了我。我是一个缓慢的typer ...

I want to be able to change the text to bold in Python. Is there a way to do that?

I have been able to change colors with termcolor but nothing so far with bold text. Has anyone tried doing it before?

解决方案

You might want to try ANSI escape sequences if your platform allows it.

Crossposting from related - and possible duplicate - SO questions: 1 2

Before you proceed, please read the warnings in the above links to make sure your operating system and Python version will allow you to properly render this.

You can define a class containing all of the ANSI escape sequences you need.

class style:
   BOLD = '\033[1m'
   END = '\033[0m'

Then print them via concatenated class calls:

print style.BOLD + 'This is my text string.' + style.END

If you don't want to go through the hassle of creating an entire class just to get bold text, you can obviously concatenate them directly instead. However, if your code is open-source and not for personal use, make sure you tell other potential readers what this does!

print '\033[1m' + 'This is my text string.' + '\033[0m'

Does this answer your question?

Edit: JYelton beat me to it. I'm such a slow typer...

这篇关于我怎样才能使文字在Python中加粗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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