I want to write screen Capital letters and index numbers in word="WElCMMerC".For example [(0,W),(1,E),(3,C),(4,M),(5,M)...]
def cap(word):
w=list(enumerate(i) for i in word if i!=i.lower())
print (w)
print(cap("WElCMMerC"))
I want to write screen Capital letters and index numbers in word="WElCMMerC".For example [(0,W),(1,E),(3,C),(4,M),(5,M)...]
def cap(word):
w=list(enumerate(i) for i in word if i!=i.lower())
print (w)
print(cap("WElCMMerC"))
You can loop over the result of enumerate
, and keep only those which have an uppercase letter (using isupper
to check for that), and return the list w
, don't print inside the function:
def cap(word):
w = [i for i in enumerate(word) if i[1].isupper()]
return w
print(cap("WElCMMerC"))
Output:
[(0, 'W'), (1, 'E'), (3, 'C'), (4, 'M'), (5, 'M'), (8, 'C')]
You made a list of enumerate
objects. Read the documentation: enumerate
is an iterator, much like range
. Rather, you need to use the enumeration.
return [(idx, letter)
for idx, letter in enumerate(word)
if letter.isupper()]
In English:
Return the pair of index and letter
for each index, letter pair in the word
but only when the letter is upper-case.