先剪除左边的空格 再剪除右边空格,方法很傻但是测试通过
Topic sourceC:\Users\hp\AppData\Local\Programs\Python\Python37\work>python 5.py length of hello is : 7 bb is: 0 s is: hello left_trim is: hello length of hello is : 7 current ii is: 6 left_trim 6 is current ii is: 5 left_trim 5 is bbb is: 4 right_trim is: hello length of hello is : 7 s 0 is s 1 is bb is: 2 s is: hello left_trim is: hello length of hello is : 5 bbb is: 4 right_trim is: hello length of hello is : 9 s 0 is s 1 is bb is: 2 s is: hello left_trim is: hello length of hello is : 7 current ii is: 6 left_trim 6 is current ii is: 5 left_trim 5 is bbb is: 4 right_trim is: hello length of hello world is : 16 s 0 is s 1 is bb is: 2 s is: hello world left_trim is: hello world length of hello world is : 14 current ii is: 13 left_trim 13 is current ii is: 12 left_trim 12 is bbb is: 11 right_trim is: hello world length of is : 0 bb is: 0 s is: left_trim is: length of is : 0 bbb is: -1 right_trim is: length of is : 4 s 0 is s 1 is s 2 is s 3 is bb is: 4 s is: left_trim is: length of is : 0 bbb is: -1 right_trim is: 测试成功!
- 1
用户5318353752
def trim(s):
trim left first
then trim right
if trim('hello ') != 'hello': print('测试失败! 1') elif trim(' hello') != 'hello': print('测试失败! 2') elif trim(' hello ') != 'hello': print('测试失败! 3') elif trim(' hello world ') != 'hello world': print('测试失败! 4') elif trim('') != '': print('测试失败! 5') elif trim(' ') != '': print('测试失败! 6') else: print('测试成功!')