+ 1
Slicing
Ok my question is in negative indexing are there exclusive and inclusive index there? Also are spaces and punctuation marks counted?
2 Antwoorden
+ 4
1. Tag the relevant programming language. It depends on the programming language how indexable collections are sliced.
2a. Write a code example to test hypotheses on slicing.
2b. Consult the documentation.
+ 2
All kinds of characters are counted.
In a string "Hello!", the indexing will be:
0 1 2 3 4 5
H e l l o !
On the other hand, negative indexing will be:
-6 -5 -4 -3 -2 -1
H e l l o !
Some examples:
a = "Hello!"
print(a[1:3]) # "el"
print(a[-5:-2]) # "ell"
Basically, the index before colon (:) is inclusive and after it is exclusive. Just remember to count from left to right.