去除str首尾空格(切片)
## str长度 循环,判断
### [:i] [i:] 记录位置点
## 方法1
def trim2(s):
s2 = ""
start = 0
end = 0
# 找出第一个不为空的项的索引
for i in range(len(s)):
if s[:i] == (i + 1) * " " or s[:i] == (i + 1) * "":
pass
else:
start = i
break
# 找出最后一个不为空的项的索引
for i in range(len(s)):
if s[i:] == (len(s) - i) * " ":
pass
else:
end = i
print(s[start:end + 1])
return (s[start:end + 1])
trim2(" mao tai")
## 第二种方法
def trim3(s):
while s[:1] == ' ':
s = s[1:]
print(s)
while s[-1:]==' ':
s = s[:-1]
trim3(" maotai3 s ")