I don't understand slicing with negative bounds in Python. How is this supposed to work?

后端 未结 7 654
臣服心动
臣服心动 2020-12-31 02:19

I am a newbie to Python and have come across the following example in my book that is not explained very well. Here is my print out from the interpreter:

>         


        
7条回答
  •  轮回少年
    2020-12-31 03:04

    If we want to print from the back-end of the string we can go for negative indexing. Indexing starts from -1.

    Example : s = 'hello world'

    s[-11:-1] = 'hello worl' s[-1:-11] = '' // beginning value should be lower(i.e., in this case -1 greater than -11) if it is greater it won't print anything.

提交回复
热议问题