Discuss / Python / 交作業,方法笨了點,總算是寫出來了

交作業,方法笨了點,總算是寫出來了

Topic source

def trim(s):

    start = 0

    while s[start:start+1] == ' ':

      start = start + 1

    end = -1

    while s[end:] == ' ':

        end = end - 1

    if  end == -1:

        end = 0

    ends = len(s) + end

    news = s[start:ends]

    print(f'开始截取索引:{start}, 结束索引位置:{ends}, 去掉空格的值为: {news}')

    return news


  • 1

Reply