def trim(s):
#处理极端值:空字符串与空格字符串
bool_s = bool(s)
if bool_s:
for x in s:
bool_s = bool_s and x == ' '
return ''
else:
#获取非空字符串索引,通过索引切片达到去除两端空格目的
count = 0
count_end = -1
#获取第一个非空字符索引值
while s[count] == ' ':
count += 1
#获取最后一个非空字符索引值
while s[count_end] == ' ':
count_end -= 1
#通过切片返回新字符串
if count_end == -1:
return s[count:]
return s[count:count_end+1]
Sign in to make a reply
南丁叔叔
def trim(s):
#处理极端值:空字符串与空格字符串
bool_s = bool(s)
if bool_s:
for x in s:
bool_s = bool_s and x == ' '
if bool_s:
return ''
else:
return ''
#获取非空字符串索引,通过索引切片达到去除两端空格目的
count = 0
count_end = -1
#获取第一个非空字符索引值
while s[count] == ' ':
count += 1
#获取最后一个非空字符索引值
while s[count_end] == ' ':
count_end -= 1
#通过切片返回新字符串
if count_end == -1:
return s[count:]
else:
return s[count:count_end+1]