Python reverse-stride slicing

前端 未结 8 647
后悔当初
后悔当初 2020-12-02 20:22

A specific example of my question is, \"How can I get \'3210\' in this example?\"


>>> foo = \'0123456\'
>>> foo[0:4]
\'0123\'
>>> foo[::-1]
\'6543210\'
>>>          


        
8条回答
  •  北荒
    北荒 (楼主)
    2020-12-02 21:19

    If you're looking for something a little more human-readable than extended slice notation:

    >>> foo = '0123456'
    >>> ''.join(reversed(foo[0:4]))
    '3210'
    

提交回复
热议问题