0

Python Challenge Query

I was doing some python challenges and recently found this but i am getting stuck.. Pls help that which blocks of code shall i use and what would be the final thing. Here is the Challenge: Challenge 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].

6th Aug 2020, 11:48 AM
Kunsh-Tyagi
Kunsh-Tyagi - avatar
14 Respostas
0
s = "ThE cOoL STRINg” b= [] for x in s: if x.isupper() is True: b.append(s.index(x)) print(b)
6th Aug 2020, 1:22 PM
madeline
madeline - avatar
+ 2
@madeline It worked, thank you so much for your help.
8th Feb 2021, 7:07 AM
BasharGh123
BasharGh123 - avatar
+ 1
Thanks a lot @madeline
10th Aug 2020, 3:36 AM
Kunsh-Tyagi
Kunsh-Tyagi - avatar
+ 1
hey I'm facing the same problem and when I do the right thing and two letters are from the same string the second one outputs its index as the the first one, so an help would be highly appreciated, thank you.
7th Feb 2021, 5:57 PM
BasharGh123
BasharGh123 - avatar
+ 1
BasharGh123 im not sure why the code has trouble, but this one seems to work a = "cOOl" b = [] for x in range(0, len(a)): if a[x].isupper() == True: b.append(x) print(b)
8th Feb 2021, 12:14 AM
madeline
madeline - avatar
0
isupper() , index() and a for loop should be what you need..
6th Aug 2020, 12:09 PM
madeline
madeline - avatar
0
Pls explain in breif @madeline
6th Aug 2020, 12:51 PM
Kunsh-Tyagi
Kunsh-Tyagi - avatar
0
of course, you could create a for loop to cycle through the string and if isupper() is true, return the index number using index()
6th Aug 2020, 12:55 PM
madeline
madeline - avatar
0
fyi: you may also need to create a new array if you’d like to print them all at once! good luck (:
6th Aug 2020, 1:18 PM
madeline
madeline - avatar
0
BasharGh123 no problem! glad it worked for you â˜ș have fun coding!!
8th Feb 2021, 10:15 AM
madeline
madeline - avatar
0
def capital_indexes(word): return [i for i in range(len(word)) if word[i].isupper()]
1st Oct 2021, 3:54 AM
Naga Satish
0
def capital_indexes(word): return [i for i in range(0, len(word)) if word[i].isupper()]
1st Oct 2021, 3:55 AM
Naga Satish
0
# Using a list comprehension. def capital_indexes(strg): return [idx for idx, char in enumerate(strg) if char.isupper()] print(capital_indexes("HoRacE")) # => [0, 2, 5]
22nd Jul 2024, 1:27 PM
John Swindin
John Swindin - avatar
- 1
def capital_indexes(name): index_list = [] for i in range(len(name)): if name[i].isupper(): index_list.append(i) return index_list print(capital_indexes("TEsT")) print(capital_indexes("HeLlO"))
10th Dec 2021, 11:29 AM
Venki Bolisetti