python中心字符串使用格式说明符 [英] python centre string using format specifier

查看:206
本文介绍了python中心字符串使用格式说明符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Message =你好,欢迎!\\\
这是一些应该是中心!

是的,这只是一个测试语句...

我试图用一个默认的终端窗口(即宽度为80),用下面的语句:

  print('{:^ 80}'。format(Message))

p>

 您好,欢迎光临! 
这是一些应该居中的文本!

我期待的是:

 您好,欢迎光临! 
这是一些应该居中的文本!

有什么建议?

解决方案

 '\\\
'.join('{: ('\\\
'))


I have a string called Message.

Message = "Hello, welcome!\nThis is some text that should be centered!"

Yeah, it's just a test statement...

And I'm trying to centre it for a default Terminal window, i.e. of 80 width, with this statement:

print('{:^80}'.format(Message))

Which prints:

           Hello, welcome!
This is some text that should be centered!           

I'm expecting something like:

                                Hello, welcome!                                 
                   This is some text that should be centered!                   

Any suggestions?

解决方案

You need to centre each line separately:

'\n'.join('{:^80}'.format(s) for s in Message.split('\n'))

这篇关于python中心字符串使用格式说明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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