Python 2.7 严格条
Python 2.7 Strict strip
我有一个字符串string = 'some.value:so this-can be:any.thing, even some.value: too'
我想去掉左边的'some.value:'
。
我失败的尝试:
>>> string.lstrip('some.value:')
' this-can be:any.thing, even some.value: too'
>>> string.replace('some.value:','')
'so this-can be:any.thing, even too'
>>> string.split(':')[1]
'so this-can be'
预期输出:so this-can be:any.thing, even some.value: too
我认为最接近我的答案是使用 lstrip()
。我怎样才能告诉 lstrip()
完全去除整个短语?
[!] 最好不要 使用任何库!
注:有类似的question但是答案不适用我
我们检查要剥离的字符串是否为开头,如果是则剪切字符串:
def strip_from_start(strip, string):
if string.startswith(strip):
string = string[len(strip):]
return string
print(strip_from_start('value:', 'value: xxx value: zzz'))
# xxx value: zzz
我有一个字符串string = 'some.value:so this-can be:any.thing, even some.value: too'
我想去掉左边的'some.value:'
。
我失败的尝试:
>>> string.lstrip('some.value:')
' this-can be:any.thing, even some.value: too'
>>> string.replace('some.value:','')
'so this-can be:any.thing, even too'
>>> string.split(':')[1]
'so this-can be'
预期输出:so this-can be:any.thing, even some.value: too
我认为最接近我的答案是使用 lstrip()
。我怎样才能告诉 lstrip()
完全去除整个短语?
[!] 最好不要 使用任何库!
注:有类似的question但是答案不适用我
我们检查要剥离的字符串是否为开头,如果是则剪切字符串:
def strip_from_start(strip, string):
if string.startswith(strip):
string = string[len(strip):]
return string
print(strip_from_start('value:', 'value: xxx value: zzz'))
# xxx value: zzz