python之切片

https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431756919644a792ee4ead724ef7afab3f7f771b04f5000#0

 

代码:

def trim(s):
    if s == '':  # 判断是否没有值
        return s
    else:
        while s != '' and s[-1:] == ' ':  # 判断最后一个值是否为空格
            s = s[:-1] # 若最后一个值是空格,则切掉最后一个数
        while s != '' and s[0] == ' ': # 判断第一个值是否为空格
            s = s[1:] # 若第一个值是空格,则切掉第一个数
        return s

if trim('hello  ') != 'hello':
    print('Failure!')
elif trim('  hello') != 'hello':
    print('Failure!')
elif trim('  hello  ') != 'hello':
    print('Failure!')
elif trim('  hello  world  ') != 'hello  world':
    print('Failure!')
elif trim('') != '':
    print('Failure!')
elif trim('    ') != '':
    print('Failure!')
else:
    print('Done!')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值