Python:字符串格式化程序对齐中心 [英] Python: String Formatter Align center
本文介绍了Python:字符串格式化程序对齐中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
print('%24s' % "MyString") # prints right aligned
print('%-24s' % "MyString") # prints left aligned
如何在中心打印?有没有快速的方法来做到这一点?
How do I print it in the center? Is there a quick way to do this?
我不希望文本位于屏幕中央.我希望它位于 那个 24 个空格的中心.如果我必须手动完成,那么添加相同的编号背后的数学是什么?文本前后的空格?
I don't want the text to be in the center of my screen. I want it to be in the center of that 24 spaces. If I have to do it manually, what is the math behind adding the same no. of spaces before and after the text?
推荐答案
使用新式 format
方法代替旧式 %
操作符,后者不t 具有居中功能:
Use the new-style format
method instead of the old-style %
operator, which doesn't have the centering functionality:
print('{:^24s}'.format("MyString"))
这篇关于Python:字符串格式化程序对齐中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文