我想从一个字符串中删除前面的字符。是否有类似这样的函数可以实现?
>>> a = "BarackObama"
>>> print myfunction(4,a)
ckObama
>>> b = "The world is mine"
>>> print myfunction(6,b)
rld is mine
我想从一个字符串中删除前面的字符。是否有类似这样的函数可以实现?
>>> a = "BarackObama"
>>> print myfunction(4,a)
ckObama
>>> b = "The world is mine"
>>> print myfunction(6,b)
rld is mine
可以,只需使用切片:
>> a = "BarackObama"
>> a[4:]
'ckObama'
这里有文档 http://docs.python.org/tutorial/introduction.html#strings
这个函数可以是:
def cutit(s,n):
return s[n:]
然后你可以像这样调用它:
name = "MyFullName"
print cutit(name, 2) # prints "FullName"
>>> a = "BarackObama"
>>> a[4:]
'ckObama'
>>> b = "The world is mine"
>>> b[6:10]
'rld '
>>> b[:9]
'The world'
>>> b[:3]
'The'
>>>b[:-3]
'The world is m'
a = 'BarackObama'
a[4:] # ckObama
b = 'The world is mine'
b[6:] # rld is mine