PYTHON: The Index() Function.

Code-Parser
302 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

PYTHON: The Index() Function.

In this playground, we are going to take a look at Python's built-in index() function.

Before we do that, I want to make clear that the index() function can be used for Strings, Lists and Tuples. We will look at how the function is used for each of those.

I'd also like to make clear that the index() function works for 0-based indexes.

# A "0-based index" is an index that begins at 0.

string = "abcde"
# "a" is indexed 0, "b" is indexed 1, "c" is indexed 2, etc.

list = ["a", "b", "c", "d", "e"]
# "a" is indexed 0, "b" is indexed 1, "c" is indexed 2, etc.

Now, let's begin.

Program 1: Using index() on Strings

We can use the index() function to find the position (or index) of the first character of a substring in a string:

# Syntax:
string.index(substring, start, end)

Here's an example of the function in action:

We can also use it to find the position (or index) of first occurence of a character in a string:

Did You Know?

The index() function does almost exactly the same thing as Python's find() function.

This is true, but there is one slight difference.

index() will raise an error, while find() will return -1.

Program 2: Using index() on Lists

We can also use the index() function to find the position (or index) of an element in a list.

# Syntax:
list.index(element)

Here's an example of the function in action:

Program 3: Using index() on Tuples

We can also use the index() function to find the position (or index) of a value in a tuple.

# Syntax:
tuple.index(value)

Here's an example of the function in action:

Conclusion:

In this playground we:

  • Took a look at Python's index() function.
  • Saw it in action working with Strings, Lists, and Tuples.
  • Compared it to Python's find() function.

Please feel free to use this code and implement it in CoC's or programs you make.

Thanks for reading, I hope this playground has been helpful.

Links and Further Information

Here is a "list" of links that you can visit to get further information on the index function and the find function:

  1. https://www.w3schools.com/python/ref_string_index.asp
  2. https://www.w3schools.com/python/ref_list_index.asp#gsc.tab=0
  3. https://www.w3schools.com/python/ref_tuple_index.asp
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content