Discuss / Python / 切片的练习题

切片的练习题

Topic source

TZNTT

#1 Created at ... [Delete] [Delete and Lock User]
def trim(s):    start = 0    end = len(s) - 1    while start <= end:        # 移动条件        if s[start] == ' ':            start += 1        if s[end] == ' ':            end -= 1        # 循环跳出条件        if s[start] != ' ' and s[end] != ' ':            break    # 如果 start > end ,说明整个字符串都是空格,或者说是空字符串    if(start > end):        s = ''    else:        s = s[start:end+1]    return s

  • 1

Reply