Discuss / Python / 先找出首尾索引,然后最后取字符串

先找出首尾索引,然后最后取字符串

Topic source

辰川时羽

#1 Created at ... [Delete] [Delete and Lock User]

def trim(s):

start = 0

end = 0

for index in range(0, len(s)):

if s[index] != ' ':

start = index

break

for index in reversed(range(0, len(s))):

if s[index] != ' ':

end = index

end = end + 1

break

return s[start:end]

辰川时羽

#2 Created at ... [Delete] [Delete and Lock User]

网页 Python代码运行助手 暂时用不了,直接复制过来没缩进。。重新发

def trim(s):
    start = 0
    end = 0
    for index in range(0, len(s)):
        if s[index] != ' ':
            start = index
            break
    for index in reversed(range(0, len(s))):
        if s[index] != ' ':
            end = index
            end = end + 1
            break
    return s[start:end]

  • 1

Reply