Remove n characters from a start of a string

前端 未结 4 1009
故里飘歌
故里飘歌 2021-02-07 06:50

I want to remove the first characters from a string. Is there a function that works like this?

>>> a = \"BarackObama\"
>>> print myfunction(4,a         


        
相关标签:
4条回答
  • 2021-02-07 07:08

    Yes, just use slices:

     >> a = "BarackObama"
     >> a[4:]
     'ckObama'
    

    Documentation is here http://docs.python.org/tutorial/introduction.html#strings

    0 讨论(0)
  • 2021-02-07 07:17

    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"
    
    0 讨论(0)
  • 2021-02-07 07:19
    a = 'BarackObama'
    a[4:]  # ckObama
    b = 'The world is mine'
    b[6:]  # rld is mine
    
    0 讨论(0)
  • 2021-02-07 07:27

    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/

    0 讨论(0)
提交回复
热议问题