Python: Is there an equivalent of mid, right, and left from BASIC?

后端 未结 7 1338
再見小時候
再見小時候 2021-01-31 15:11

I want to do something like this:

    >>> mystring = \"foo\"
    >>> print(mid(mystring))

Help!

7条回答
  •  梦毁少年i
    2021-01-31 15:40

    You can use this method also it will act like that

    thadari=[1,2,3,4,5,6]
    
    #Front Two(Left)
    print(thadari[:2])
    [1,2]
    
    #Last Two(Right)# edited
    print(thadari[-2:])
    [5,6]
    
    #mid
    mid = len(thadari) //2
    
    lefthalf = thadari[:mid]
    [1,2,3]
    righthalf = thadari[mid:]
    [4,5,6]
    

    Hope it will help

提交回复
热议问题