在python中打印下标 [英] Printing subscript in python
本文介绍了在python中打印下标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Python 3.3 中,有没有办法在打印时在字符串下标中制作一部分文本?
例如H₂(H 然后是下标 2)
解决方案
如果你只关心数字,你可以使用 str.maketrans()
和 str.translate()
方法:
请注意,这在 Python 2 中不起作用 - 请参阅 Python 2 maketrans() 函数不适用于 Unicode解释为什么会这样,以及如何解决它.
In Python 3.3, is there any way to make a part of text in a string subscript when printed?
e.g. H₂ (H and then a subscript 2)
解决方案
If all you care about are digits, you can use the str.maketrans()
and str.translate()
methods:
>>> SUB = str.maketrans("0123456789", "₀₁₂₃₄₅₆₇₈₉")
>>> SUP = str.maketrans("0123456789", "⁰¹²³⁴⁵⁶⁷⁸⁹")
>>> "H2SO4".translate(SUB)
'H₂SO₄'
Note that this won't work in Python 2 - see Python 2 maketrans() function doesn't work with Unicode for an explanation of why that's the case, and how to work around it.
这篇关于在python中打印下标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文