Solving Python Challenges **Capital indexes** Write a function named `capital_indexes`. The function takes a single parameter, which is a string. Your function should return a list of all the indexes in the string that have capital letters. For example, calling `capital_indexes("HeLlO")` should return the list `[0, 2, 4]`. Source of Challenge: pythonprinciples.com https://pythonprinciples.com/ **Solution:** `def capital_indexes(string):` `lis =[]` `for i in range(len(string)):` `if string[i].upper() ==string[i]:` `lis.append(i)` `return lis` `print(capital_indexes("HeLlO"))` **Explanation:** The purpose of this program is to identify Capital letters in a string. Say, In a string "ABcdeF" it should give "ABF" as a result. But, there is a twist in this challenge, it sure is asking us to identify capital letters but not asking to display them, instead it is asking to display the index where these letters are stored. So, As in our Example "ABcdeF", the result would be "0 1 5" instead of "A B F". And, before you ask any questions, let me tell you that in programming languages index starts from 0. So, index for ABcdeF is A=0 B=1 c=2 d=3 e=4 F=5 So, now that we know what is asked, let's take a look at the solution First we created a function named `capital_indexes`(You can name it anything) which takes a **string** as input (value to be tested). Secondly, we created an empty list " **[]** " named **lis** (I will explain "Why" later). Then, in the third step we used a loop __**_____Explanation**__**_____** What is a loop? Loop is something that keeps repeating itself. In programming, it means that a "piece of code" will run repeatedly until the "specified condition" is met. For example; Writing, `for i in range(11):` `print("Hi")` will print "Hi" 11 times. As the condition for loop was to run 11 times And, remember, **range()** means **[ goal -1 ]** means **[ 11 -1 = 10 ]** Why? Because index starts from "0". So, basically, **11** means **0-10 (** which ultimately means 11 **)** __**_____Explanation**__**_____** **Note:** you might be wondering what is "`i`". It is just a variable that kind of keeps track of every iteration means from (0 -10) When the loop runs for the first time, i will be `i=0` while in the last iteration `i` will be `i =10` _______________________________________________ `for i in range(len(string)):` So far, I believe that now you have an idea about the condition of the loop we are using. What we mean by saying `for i in range(len(string)):` is that this loop will keep running the same code until it reaches **len(string).** __**_____Explanation**__**_____** **What is len(string)** **len()** is a function that tells us the length of any string and "**string**" in this case is the name of our string "**HeLlo**". For example for our string `a= ABcdeF`, if we print **len(a),** it will give us **6**. Yes, it would return **6** not **5** because len() function do not follows the index ( which starts from 0). So, now if we again look at **len(string)** where **string** is **"HeLlO",** we can easily identify that it is `len(string) = 5` In other words , `for i in range(len(string)):` simply means `for i in range(5):` and you already know that in range() 5 means (0-4). As a result this loop will run till 4th index ( and 4th means last letter) H =0 e=1 L=2 l=3 O=4 __**_____Explanation**__**_____** _________________________________________________ 5. Now, we know for how long the code will run but we don't know what exactly the code is doing. So, let's find out `for i in range(len(string)):` `if string[i].upper() ==string[i]:` `lis.append(i)` `return lis` In the loop we used an `if condition` __**_____Explanation**__**_____** What is if-else condition? `if statement` is simply a conditioned statement which runs only if the condition is **True**. And `else` is another statement that runs when any other condition is not met. In simple words if-else says that **if codition is met:** **do this** **else:** **do this** If I explain it using example `a = 10` `if a==10:` `a=a+10` `print(a)` `else:` `print(a)` the above example says that if value of `a` is `10` then add 10 to it's value and then print it, and if `a` not equal to `10` then simply print value of `a`. So, as we know that condition is **True** so the above program will print **"20".** __**_____Explanation** __**_____** Now, if again look at our if condition `if string[i].upper() ==string[i]:` it is obvious, that this **if condition** simply compares value of `string[i].upper()` to value of `string[i]` , and if both are equal (means the condition is **True**) it appends(adds), `i` to `lis`( which is our empty list **[]**, Remember?) And, then at the end, when all the iterations( 0-4) are completed, it returns value of list `lis` (which is now modified). __**_____Explanation**__**_____** I know I have dumped a lot of stuff on you in the 5th step, but don't worry I will explain it. **What is** `string[i]` you already know `i` is the value of index for every iteration right? So writing it as `string[i]` means referring to the element stored on that specific index( and as i changes constantly, it ultimately iterates over all the elements of the string). So if we look at it's working for the first iteration `string[i]` will be `'H'`. Because `i=0` 2. for the second iteration `string[i]` will be `'e'`. Because `i=1` and it will iterate till `i` is reaches `4` , which means `string[i]` where `i = 4` will be `'O'` **What is** `string[i].upper()` So, we know what is `string[i]` right? It simply means all the elements of string one by one. But, what is `string[i].upper()`? Let's see, **upper()** is just a built-in function that converts the whole string in uppercase. For Example: In our string `a=ABcdeF` if we print `a` as `print(a)`, it would just display `ABcdeF` but, if we `print(a.upper())`, it will print `ABCDEF` So, what it means is that `string[i].upper()` also refers to all the elements of string one by one. The only difference is that it has all the elements in uppercase. So for all iterations of string[i] = **H e L l O** all of iterations of string[i].upper() will be: **H E L L O** _______________________________________________ so, if we again look at the if condition `if string[i].upper() ==string[i]:` we know that we are comparing each element of `string[i]` to each element of string[i].upper() and if the element at `string[i]` is equal to element at `string[i].upper()` , means that it is also captital letter such as 'H', 'L' and 'O' in our case. it simply uses `append()` function to append `i` (index of that specific element for example `0`for `'H'`) to the `lis`. And we already know `lis`is the name of our empty list. **Note: append()** is just a `list` function that adds an element at the end of list (and when the list is empty it ultimately becomes the first element). And then when all the elements have been iterated, it returns `lis` ( which, at the moment, is not empty, but has i ( 0 which is'H' , 2 which 'L' and '4' = which is 'O') stored in it. And that's how our loop ends and the function is finished. And yeah the last line `print(capital_indexes("HeLlO"))` well, guess what, it is first line which is calling the function "`capital_indexes`" and giving it the value "`HeLlO`" which is stored in variable name "`string`". And that's the moment when the whole function is called and the loop is run. and the reason we put this function call `capital_indexes("HeLlO")`in a `print()` statement is so that it will display the result. Because using `return` only returns the result which can be stored in a variable or can be directly printed, but it won't print anything on it's own. So, here, it ends. I never thought it would take this long to explain a simple function but it ended anyway. So, I hope you like it. And if you do, mention it in the comments, thanks.
Log in to join in Reading is open to everyone. Replying needs an account.
No comments yet