+ 3

Slicing

Ok my question is in negative indexing are there exclusive and inclusive index there? Also are spaces and punctuation marks counted?

25th Sep 2025, 2:45 PM
buzz queen
buzz queen - avatar
4 Answers
+ 6
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.
25th Sep 2025, 3:32 PM
Lisa
Lisa - avatar
+ 5
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.
25th Sep 2025, 3:36 PM
ᴜɴᴋɴᴏᴡɴ ᴅᴇᴄᴏᴅᴇʀ
ᴜɴᴋɴᴏᴡɴ ᴅᴇᴄᴏᴅᴇʀ - avatar
+ 1
Ok thank you all for your help
26th Sep 2025, 3:17 PM
buzz queen
buzz queen - avatar
+ 1
Negative indexing in Python Negative indexes just count from the end. Example: s[-1] = last character, s[-2] = second-to-last, etc. When you slice (s[a:b]), the start index is inclusive, the end index is exclusive, this rule is the same whether indexes are positive or negative. s = "Python" print(s[-4:-1]) # "tho" (includes -4, excludes -1) Spaces and punctuation Yes, spaces (" ") and punctuation (! , . ?) are counted as characters. Example: "Hi, you!"[3] → "," So negative indexing follows the inclusive start, exclusive end rule, and every single character (letters, numbers, spaces, punctuation) is counted in indexing.
27th Sep 2025, 4:55 AM
SITHU NYEIN
SITHU NYEIN - avatar