def trim(s):
if not isinstance(s,str):
raise TypeError('bad operand type')
if len(s) == 0:
return s
elif s.isspace() == True: #如果字符串不为空,且字符串内元素均为空格,则返回空字符串
return ''
while s[0] == ' ':
s = s[1:]
while s[-1] == ' ':
s = s[:-1]
Sign in to make a reply
dudefitslol
def trim(s):
if not isinstance(s,str):
raise TypeError('bad operand type')
if len(s) == 0:
return s
elif s.isspace() == True: #如果字符串不为空,且字符串内元素均为空格,则返回空字符串
return ''
while s[0] == ' ':
s = s[1:]
while s[-1] == ' ':
s = s[:-1]
return s