'Can someone explain how the indexing on strings work with python [closed]

Given a = "helloworld" print(a[0][0][0][0])

When I run this program, it gave me "h" as output. Can someone explain how this works??



Solution 1:[1]

[0] always gets the first character, because it is the first index of the string (or list)

So, if you do print(a[0]), it would give the first character 'h'

If you add [0] to it, it would give the first character of 'h' which is 'h' itself

So, if you keep adding the [0], it would still give you 'h'

Solution 2:[2]

In every languages when it comes to an array it means you are considering a sequence of data's all together of same dataType in a such a manner that the first element is always present at index 0 and the last element is present at the (size of the array provided by the programmer) -1, basically it is a data structure that is used to store values of homogeneous type.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 PCM
Solution 2 Rohit Sen