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

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

I want to do something like this:

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

Help!

7条回答
  •  萌比男神i
    2021-01-31 15:35

    slices to the rescue :)

    def left(s, amount):
        return s[:amount]
    
    def right(s, amount):
        return s[-amount:]
    
    def mid(s, offset, amount):
        return s[offset:offset+amount]
    

提交回复
热议问题