【发布时间】:2015-06-18 00:24:31
【问题描述】:
我想在我的代码中使用一个函数来证明字符串的合理性。我被卡住了,请看我的代码。 提前致谢。
def justify(s, pos): #(<string>, <[l]/[c]/[r]>)
if len(s)<=70:
if pos == l:
print 30*' ' + s
elif pos == c:
print ((70 - len(s))/2)*' ' + s
elif pos == r:
print (40 - len(s)*' ' + s
else:
print('You entered invalid argument-(use either r, c or l)')
else:
print("The entered string is more than 70 character long. Couldn't be justified.")
【问题讨论】:
-
它实际上是做什么的?错误?我自己会使用“%-70s”和“%70s” sprintf 风格的面具,它们更干净。中心证明可能可以通过一些额外的工作来实现
-
@BrenBarn 谢谢 :)
标签: python string function formatting justify