0
我想在我的代码中使用一个函数来验证字符串。我卡住了,请看我的代码。 在此先感谢。python中的函数,证明字符串(左,中,右)
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.")
见['str.ljust'](https://docs.python.org/2/library/stdtypes.html#str.ljust)和['str.rjust '](https://docs.python.org/2/library/stdtypes.html#str.rjust)。 – BrenBarn
它实际上做了什么?错误?我会使用“%-70s”和“%70s”sprintf式的面具,他们更干净。该中心证明可以实现一些额外的工作 –
@BrenBarn谢谢:) –