繁体   English   中英

从字符串的开头删除n个字符

[英]Remove n characters from a start of a string

我想从字符串中删除第一个字符。 有这样的功能吗?

>>> 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'

您可以在官方教程中阅读此内容和大多数其他语言功能: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM