-2

So the function len(string) is showing an additional index than what is actually displayed in the string, I thought that len() starts counting from the index 0 ?

The index length after the program is execute is shown in the terminal below

Image Example

In this example it should be a total of 8 index rather than 9

EDIT:

If len() returns the length then how comes when i print(s[0:3]) it returns 928 ?

Shouldnt it return 0, 1, 2, 3 indexes ? so should be 4 digits in total ?

Example 2

  • 4
    `len(string)` returns the number of characters in your string. In your case, 9. – Thomas Schillaci Feb 26 '20 at 12:03
  • It shows all the characters in your string.You might be mixing it up with the access to elements? That starts with a zero and therefore the last element is one less than the length. – vaeng Feb 26 '20 at 12:06
  • 1
    "In this example it should be a total of 8 index rather than 9" why? – glglgl Feb 26 '20 at 12:07
  • Your logic doesn't make sense, in a 12 inch ruler, would you say that it can only measure items up to 11 inches long? – Sayse Feb 26 '20 at 12:11

2 Answers2

3

The indices start counting from 0, but the length is still the human definition of length (it has nine total characters). In 0-up indexing languages, the first inaccessible index is the length itself.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • "In 0-up indexing languages, the first inaccessible index is the length itself." Not necessarily. I bet that the OP comes from a background of Perl, where the "length" of an array using the `$#` notation refers to the *last accessible* index. – blhsing Feb 26 '20 at 12:21
  • @blhsing: That's not the length though. The scalar interpretation of the array (`scalar @arr`) is the length, `$#arr` is just the weirdo Perl operator that specifically means "last index of the array". I'll admit there probably are languages where length and first inaccessible index don't perfectly correlate (e.g. JavaScript, where all indices are accessible, and reading beyond the length just finds more `undefined` values while assigning beyond the length increases the length), but by and large, actual length has to be one beyond the last valid index in a 0-up language by definition. – ShadowRanger Feb 26 '20 at 18:13
  • Yes I know, but I'm just speculating that that is where the OP's confusion is coming from. :-) – blhsing Feb 26 '20 at 18:14
  • Ok so how comes when i did print(s[0:3]) it returns only 928 ? – Tuyen Khong Feb 26 '20 at 21:49
  • @TuyenKhong: Slicing syntax is half-open; it's inclusive on the start index, exclusive on the end index. On top of being consistent with `range` and the like, it has the convenient feature that `end - start` gets the length of the result without needing to add 1 after (assuming the sequence is long enough to produce the whole slice). So `s[0:3]` (equivalent to `s[:3]` since `0` is the implicit start for forward slices) is slicing out indices 0, 1 and 2, but stopping "just before" index 3. [Full details on this question](https://stackoverflow.com/q/509211/364696) – ShadowRanger Feb 26 '20 at 23:16
  • @TuyenKhong: In case it's not clear, the second value in the slice is the stop index, not the length of the slice. `s[2:4]` means "slice from index 2 (inclusive) to 4 (exclusive)", not "slice from index 2, continuing for four values" like substring operations do in some other languages. – ShadowRanger Feb 26 '20 at 23:19
0

The number of characters is 9 and len returns 9 The len count does not start from 0

If it starts from zero

>>> len("12") #Real 2
1
>>> len("1") #Real 1
0
>>> len("") #Real 0 | return -1? That's why it doesn't start at zero

Nima
  • 323
  • 4
  • 13
  • Ok so how comes when i did print(s[0:3]) it returns only 928 ? – Tuyen Khong Feb 26 '20 at 21:54
  • @TuyenKhong This is different from len And it starts from zero and len ​​starts from zero But it assigns zero to empty `len([])==0 and len("")==0` – Nima Feb 27 '20 at 07:58