1.问题描述:
实现一个leftpad库,如果不知道什么是leftpad可以看样例。
2.样例:
leftpad("foo", 5)
>> " foo"
leftpad("foobar", 6)
>> "foobar"
leftpad("1", 2, "0")
>> "01"
3. 代码:
class StringUtils:
"""
@param: originalStr: the string we want to append to
@param: size: the target length of the string
@param: padChar: the character to pad to the left side of the string
@return: A string
"""
def leftPad(self, originalStr, size, padChar=' '):
# write your code here
length=len(originalStr)
ans=""
if length>=size:
ans=originalStr
else:
dif=size-length
l=list(padChar)
for i in range(dif):
ans+=l[i]
ans+=originalStr
return ans