[英]Remove n characters from a start of a string
I want to remove the first characters from a string. 我想从字符串中删除第一个字符。 Is there a function that works like this? 有这样的功能吗?
>>> a = "BarackObama"
>>> print myfunction(4,a)
ckObama
>>> b = "The world is mine"
>>> print myfunction(6,b)
rld is mine
Yes, just use slices: 是的,只需使用切片:
>> a = "BarackObama"
>> a[4:]
'ckObama'
Documentation is here http://docs.python.org/tutorial/introduction.html#strings 文档在http://docs.python.org/tutorial/introduction.html#strings
The function could be: 功能可以是:
def cutit(s,n):
return s[n:]
and then you call it like this: 然后你这样称呼它:
name = "MyFullName"
print cutit(name, 2) # prints "FullName"
Use slicing. 使用切片。
>>> 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'
You can read about this and most other language features in the official tutorial: http://docs.python.org/tut/ 您可以在官方教程中阅读此内容和大多数其他语言功能: http : //docs.python.org/tut/
a = 'BarackObama'
a[4:] # ckObama
b = 'The world is mine'
b[6:] # rld is mine
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.