-3
message = "Help me Obi-Wan Kenobi, you're my only hope."
message = message[8:][:-22]
print(message[8:], end="")

result: Kenobi

Is it the order is from left to right and after first [:] it will return the result for next [:]? any document state that it will return the result for the second [:] ?

SamLi
  • 105
  • 2
  • 11
  • 1
    `message[8:]` returns the string from index 8 onwards and `[:-22]` gives you _that_ up (but not including) the 22nd-to-last index. It would be better to just do `message[8:-22]` though. – ssp Dec 16 '20 at 08:23
  • 1
    [8:] indicates you want to start the string from 8th index position (index 0 is the first position). The -22 says you want to stop at 22nd position from the back – Joe Ferndz Dec 16 '20 at 08:24
  • 2
    Does this answer your question? [Understanding slice notation](https://stackoverflow.com/questions/509211/understanding-slice-notation) – ashraful16 Dec 16 '20 at 08:25
  • @Moosefeather two [:] can combine into one? – SamLi Dec 16 '20 at 08:26
  • 1
    The answers are more interesting than the question. All of them. What a coincidence. – skuzzy Dec 16 '20 at 08:27

4 Answers4

2

Consider the action in multiple steps.

First: the whole string

"Help me Obi-Wan Kenobi, you're my only hope."

Second. Slice the string from 8 onwards. Results in:

"Obi-Wan Kenobi, you're my only hope."

Third. Slice the result from 22 from the end. Results in:

"Obi-Wan Kenobi"

And finally, when you print you are slicing again, so a 4th step:

"Kenobi"

So you can see that the multiple square brackets don't necessarily mean multiple dimensions, but rather chained actions, each being performed on the result from the last.

In arrays of arrays this has the effect of accessing your value, in strings it just means slicing in more places.

Keldan Chapman
  • 665
  • 5
  • 21
2

message[8:] returns all elements from the 8th position from the left-hand side of the message onwards.

>>> message[8:]
"Obi-Wan Kenobi, you're my only hope."

message[8:][:-22] returns all elements from the 22nd position from the right-hand side of the message[8:] ("Obi-Wan Kenobi, you're my only hope.") onwards.

>>> message[8:][:-22]
"Obi-Wan Kenobi"

So, if print message[8:] again, it will return all elements from 8th position from the left-hand side of "Obi-Wan Kenobi" onwards which is "Kenobi".

JayPeerachai
  • 3,499
  • 3
  • 14
  • 29
2
  • message[8:] means you are taking the string from 9th char to end of the string. Obi-Wan Kenobi, you're my only hope.
  • message[8:][:-22] then you perform second slice [:-22]-ve index means you are slicing the string from the end. Here, string is sliced which takes all the char leaving 22 chars from the end. Obi-Wan Kenobi
  • so your string after message[8:][:-22] becomes Obi-Wan Kenobi.
  • At last while printing you are slicing the string again print(message[8:], end="") which takes Obi-Wan Kenobi and slice it such that it takes all the chars in the string from the 9th char, which is Kenobi
Amit Kumar
  • 613
  • 3
  • 15
0

[8:] is from where the slicing begins. [:-22] is till when the slicing is to be done, the -22 indicates that it should be sliced 22 positions backwards from the last index.