-1

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"))
Prune
  • 76,765
  • 14
  • 60
  • 81

2 Answers2

2

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')]
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
0

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.
Prune
  • 76,765
  • 14
  • 60
  • 81