Finding the Index in Python

Free Video Tutorial and Guide

In this video, we're going to look at how to find the Index in Python

Video Transcript

Hi, my name is Art, and I teach Python at Noble Desktop. In this video, I'm going to show you how you could use function range to find the index of characters in a string.

First, let's create a string here: word = "Apple". Let me remind you what index is. You could always get a letter or character from the string by its index. We always start counting at zero, so what you could do is word[0], then word[1], and then word[2]. That's how you could get a letter by letter from the string.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

Now, that's okay, however what if later we change the string? What if later it is "Banana" or something else? So we want to do this dynamically. To do this dynamically, we could do a couple of things. So first of all there is a Python built-in function, len(). If we pass here "word", it will get us the number of letters in that word, which is five. Also keep in mind that 5 is an integer.

Right, so if you're not sure, you could always run type on this len() and you see that's an integer. And let me remind you, although you could watch my previous video about this function, you could always run help on function range: help(range). Here you can find all information how to use this function.

So, function range at least requires one integer to generate a range. Right, so hopefully for us here, we have an integer which is five. So what we could do is we could actually use function range here: range(5). Let me remind you that stop is not included. So, we would usually use a for loop with range.

It could look like this: for i in range(5). Let's print i and now you see function range generated sequence of numbers in the range of that word "Apple".

Right, so keep in mind that range always returns integers, and I got technically nothing to do with each letter. But fortunately for us, we could use it as an index.

So what we could do using the same setup that I just showed you is, we could do word[i] and instead of manually passing index by index since here we have 0, 1, 2, 3, 4, and these numbers represented by variable i, we could pass here i. And you see we're getting 0, 1, 2, 3, 4, and we're fetching all these letters.

Also, you could switch this word to "Banana". Don't forget to run that cell and now that would work perfectly on "Banana". If you want to go with something like "cat" or "dog", give it a try, you see it works just fine. So you see that would be a versatile solution to find the index of each letter.

Now, let's get back to "Apple" and I'll show you one more thing using range. Remember, in my previous video when I was discussing function range, I showed you how you could go in the ascending order.

Now, in this case, suppose we want to go from 4 to 0, and that would mean that we want to go from "e" to "a" in reverse order. Right, so how can we do it? Pretty simple. So, function range again requires three arguments: start, stop, step. Where are we going to start?

We're going to start at 4. And len("word") gets us five, so we could decrease this by one, so that's going to be 4. And then we would need to do minus one, because stop is exclusive, and if I use zero, I'm going to lose this guy. I don't want to lose this guy, right? So minus one, and step is -1 because we want to go in reverse. And now you see we're going in descending order, right? So "e", "l", "p", "p", "a".

So, you could actually use these two. You could actually use this in a bunch of different solutions. For example, in palindrome, a very common problem, and a very common question during a job interview. So, this is how you could do it. All right, so watch my other videos. I'm going to show you function enumerate.

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram