python输出文本居中_Python Logic-使用句点将文本居中

我要this intro to python course online

问题显示为:

For this program, the first line of input is an integer width. Then, there are some lines of text; the line “END” indicates the end of the text. For each line of text, you need to print out a centered version of it, by adding periods .. to the left and right, so that the total length of each line of text is width. (All input lines will have length at most width.) Centering means that the number of periods added to the left and added to the right should be equal if possible; if needed we allow one more period on the left than the right. For example, for input

13

Text

in

the

middle!

END

正确的输出将是

.....Text....

......in.....

.....the.....

...middle!...

给出的提示是:

For input line length of L, you should add (width-L)\2 periods to the right side

到目前为止,这是我的代码:

width = int(input())

s1 = input()

periods_remain = width - len(s1)

L = periods_remain

periods_rtside = (width-L)//2

periods_leftside = width - periods_rtside

periods_rt_str = '.' * periods_rtside

periods_left_str = '.' * periods_leftside

line1 = periods_left_str + s1 + periods_rt_str

我的line1结果看起来像是“ ……… Text ..”而不是… Text ….

我的问题似乎是L.我不确定如何定义L.谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值