九州体育网 - 十年信誉网站

关于作者

Discuss / Python / 一种比较麻烦的方法

一种比较麻烦的方法

Topic source

笨笨熊佳丽

#1 Created at ...
# -*- coding:utf-8 -*-

def trim(s):
    count = 0
    for n in s:
        if n == ' ':
            count = count + 1
        else:
            break
    s = s[count:]
    count = 0
    for n in s[-1:]:
        if n ==' ':
            count = count + 1
    s = s[:-count]

    return s

print(trim('hello   '))
print(trim('   hello'))
print(trim('   hello   '))
print(trim('   hello world   '))

  • 1

Reply

WARNING: You are using an old browser that does not support HTML5. Please choose a modern browser (Chrome / Microsoft Edge / Firefox / Sarafi) to get a good experience.