0

I've been learning slice() and trying all the possibities that I can think of to practice, but I came across this question.

In this example which I made, I used 3 websites:

website1 = "https://www.google.com"
website2 = "https://www.youtube.com"
website3 = "https://www.arrozcovo.com"

And used the slice function: slice(12,-4), so only the name of the website would appear on the following print:

slice_exemplo = slice(12,-4)

print(website1[slice_exemplo]) // google
print(website2[slice_exemplo]) // youtube
print(website3[slice_exemplo]) // arrozcovo

But when attempting the opposite for example: slice(-4,5), I thought that the print would be something like: .comhttps

But that didn't occur, I'm asking just out of curiosity, I know that the answer could be: "python just doesn't work like that", but if the answers could be more explanatory, I would appreciate it.

icyknight
  • 35
  • 6
Jorgityo
  • 11
  • 7
  • 3
    A negative index is just a shortcut for a positive index, that far from the end of the sequence. Slices do not care whether you used a positive or negative index, all they care about is whether the start index is earlier in the sequence than the end index (or vice versa, in the case of a negative step). – jasonharper Aug 16 '23 at 23:40
  • Slices don't wrap around. – Barmar Aug 17 '23 at 00:01
  • This is specific to python builtin sequence types, but also commonly implemented by others. Look at [Common Sequence Operations](https://docs.python.org/3/library/stdtypes.html#common-sequence-operations) note 4: _If i or j is negative, the index is relative to the end of sequence s: len(s) + i or len(s) + j is substituted._. -4 is converted to a positive number for the slice. – tdelaney Aug 17 '23 at 00:10
  • A slice like `slice(-10, 18)` will give you the same result for your `google` string, but it will give shorter, rather than longer results for the other two URLs. – Blckknght Aug 17 '23 at 00:10
  • @jasonharper please correct me if im wrong, but when you said: "(or vice versa, in the case of a negative step)", this would mean "whether the start index is LATER in the sequence than the end index"? and the sequence would be something like https://imgur.com/a/1zXXBYW (0 1 2 3 [...] -1 -2 -3)? Because i thought that for example (0,6) its in a forward line,like the (12,-4)that "stops" adding because of the -4 that excludes, and (-8,-5) in the same lane of thought,but inverting the sequence of the arrow(that i made in the other image): https://imgur.com/a/EHJ86la, and because of that – Jorgityo Aug 17 '23 at 03:38
  • and because of that the (-4,5) dont work because as the -4 is going back to -1 for example, it cant go back to 5, because the 5 is there to exclude a part of the index that not even there https://imgur.com/a/UQvQh7v, is that it? – Jorgityo Aug 17 '23 at 03:42

1 Answers1

0

This is the structure of slice(): slice(start, stop, step)

  • start : Starting integer where the slicing of the object starts.
  • stop : Integer until which the slicing takes place.
  • step : Integer value which determines the increment between each index for slicing.

When you provide a call like slice(-4,5), it is treated as slice(start=-4, stop=5, step=None) which does not make any postive lengthed slice because of out-of-sequence start index and stop index value and no negative step defined. That's why you are not seeing any output.

To generate .comhttps try:

print(website1[-4:] + website1[:5])
#       start -4         stop=5