python字体加粗代码_我怎样才能使文字在Python中加粗?

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...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值