8

Perhaps, it sounds like a stupid question. However, I have to learn what index is because I want to learn python.

For example, in a website, I saw this:

The method find() determines if string str occurs in string, or in a substring of string if starting index beg and ending index end are given.

http://www.tutorialspoint.com/python/string_find.htm

What does "index" mean here? I would like you to explain it to me like you are explaining something to a child. Because my comprehension is somewhat bad. Anyway. You can even provide examples to explain what index is.

Many thanks.

3
  • A few answers have been posted; please take a look and accept one (if it was helpful) or leave a comment if the answer didn't quite clear things up. Thanks! Commented Nov 30, 2013 at 3:29
  • All of the answers are helpful. Choosing one of them would be unfair for other answers. Commented Dec 4, 2013 at 0:02
  • 1
    That may be, but you might not be aware that the rate at which you accept answers in your questions is tracked, and if that rate is low, people may be turned off from answering your questions because it might seem like inattentiveness or ingratitude on your part not to accept answers. Commented Dec 4, 2013 at 21:28

5 Answers 5

11

An index, in your example, refers to a position within an ordered list. Python strings can be thought of as lists of characters; each character is given an index from zero (at the beginning) to the length minus one (at the end).

For the string "Python", the indexes break down like this:

P y t h o n
0 1 2 3 4 5

In addition, Python supports negative indexes, in which case it counts from the end. So the last character can be indexed with -1, the second to last with -2, etc.:

 P  y  t  h  o  n
-6 -5 -4 -3 -2 -1

Most of the time, you can freely mix positive and negative indexes. So for example, if you want to use find only from the second to second-to-last characters, you can do:

"Python".find("y", beg=1, end=-2)
Sign up to request clarification or add additional context in comments.

Comments

7

"index" is meant as "position".

Let's use find() as an example: find() will look for a string in another string. It will start its search at the beginning index called beg and will end its search at the end index called end. So it will only search between beg and end. Usually (by default) beg is 0 (which means it is the first character in the string) and end is the length of the string minus one (which means it is the very last character in the string). So an index is just a position (not only in a string, e.g. also in an array).

Comments

4

Consider this string "Hello". If you wanted to point out to some of it's characters like e you would need an index, which is a position number. Indices in python start counting from zero. So the index of letter e in "Hello" is 1.

Try to run this line of code:

print "Hello".find("e");

It should return you 1.

You can further play with it an run it again what it does. Try to replace "e" with "H", next try something that isn't in "Hello".

Comments

4

If a street with houses is an analogy for an array with elements, then house number is analogy for index. However, Python uses numbers from zero.

The built in sequence types in Python (ver. 3) are strings (sequences of unicode characters), bytes (sequences of small integers from zero to 255, i.e. of byte values), tuples (sequences of whatever elements), and lists (sequences of whatever elements). All of them are implemented via arrays; this way they can be indexed as is usual for arrays in any (other) programming language.

If a is such an array (i.e. one of the above sequence types), then a[0] refers to the content of the first house in the street, a[1] to the second house, etc. Some of the sequences can be modified (here only the lists), therefore they are called mutable. Some of the sequences cannot be modified (strings, bytes, tuples), therefore they are called immutable.

For mutable sequence type objects, the elements can be changed via assignment like lst[0] = 3, the elements of the immutable sequences can only be read and the value used, like print(s[3]).

For lists and tuples the elements are not stored directly inside the array. Only the references to the target objects are stored inside. The target object is accessed indirectly (one hop along the reference). Think in terms that you go to the indexed house, and the person inside tells you where is the real content (in another house, not in this street). In such case, even the element from the (immutable) tuple like t[5]--i.e. the reference--can be used to change the target object content... if the target object is mutable.

Comments

0

In python, the indexing start from 0. So basically lst[0] means first element in list (lst), lst[1] means second element and so on.... Also we can access last element as lst[-1], lst[-2] is second from the last and so on...In other programming language like R, indexing starts from 1 instead of 0, so we need to be careful with that.

1 Comment

Thank your for contributing. An index simply notes a position in a list like item. It is important to note that python actually indexes between list like items. For example, take the list, my_list = ['a', 'b', 'c]. is indexed like 0 'a' 1 'b' 2 'c'. If you tell python my_list[0], it implies my_list[0:1]. ,meaning the list items between 0 and 1 indexes. However my_list[0:2] returns ['a', 'b'], not list items 0 through 2. Please refer to Python docs and consider elaborating your post. python-reference.readthedocs.io/en/latest/docs/brackets/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.