Daily · February 8, 2023

Python Index

Indexes are perfect to use in Python when you have to locate a certain part in a string.

For example:

abc = "a b c"
index = abc.index("b")
print("Index of substring:", index)

In the code above, abc is put into index of abc. The quotations say b, so where is b? Start from the start, and count. a is 1, space is 2, b is 3. So b is located in 3.

Here is another example:

q = "queue"
n = q.index("u")
print("n")

Now we find “u” in queue. q is 1. u is 2. So print 2. We don’t print 4 because that u is after.