def trim(str):
while str[:1]==' ': #首开始往后对比
str=str[1:]
while str[-1:]==' ': #尾开始往前对比
str=str[:-1]
return str
print(trim(' hollow word ')) #hollow word
Python切片,实现一个trim()函数,去除字符串首尾的空格

最新推荐文章于 2022-09-15 01:04:40 发布