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].
14 ответов
0
s = "ThE cOoL STRINg”
b= []
for x in s:
if x.isupper() is True:
b.append(s.index(x))
print(b)
+ 2
@madeline It worked, thank you so much for your help.
+ 1
Thanks a lot @madeline
+ 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.
+ 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)
0
isupper() , index() and a for loop should be what you need..
0
Pls explain in breif @madeline
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()
0
fyi: you may also need to create a new array if you’d like to print them all at once! good luck (:
0
BasharGh123 no problem! glad it worked for you ☺️ have fun coding!!
0
def capital_indexes(word):
return [i for i in range(len(word)) if word[i].isupper()]
0
def capital_indexes(word):
return [i for i in range(0, len(word)) if word[i].isupper()]
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]
- 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"))