求助python问题

qq_38663855 qq_38663855 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
写一个去字符串头尾空格的程序
def trim(s):
if len(s) > 0:
while s[-1:] == ' ':
s = s[:-1]
while s[0] == ' ':
s = s[1:]
return s

会有
File "d:\pyhthoncode\hello.py", line 5, in trim
while s[0] == ' ':
IndexError: string index out of range
这样的报错,
但是把这行s[0]变成s[:-1],就没错了,想问一下为什么不能写s[0]呢?
...全文
评论 3
收藏 已收藏
分享
返回顶部
qq_38663855
其他开发语言 3,425 成员 15,624 内容