Home > Blockchain >  Program :find Capital letter in word in python
Program :find Capital letter in word in python

Time:04-24

I have a challenge that find index of capital letter in word. For example "heLLo" : the output : [2,3]

def capital_indexes():
    word =input("enter your word :")
    s=list(word)
    a =[]
    print(s)
    for i in s:
        if (i.isupper()):
            a.append(s.index(i))
    print(a)

capital_indexes()

This program is works. But when i input a a word that first and second letter are capital and same, the output is [0,0].

CodePudding user response:

You can use enumerate:

def capital_indexes():
    word =input("enter your word :")
    a = []
    for i, j in enumerate(word):
        if (j.isupper()):
            a.append(i)
            print(j)
    print(a)

capital_indexes()

Output (with heLLo input):

L
L
[2, 3]

You can also condense this with list comprehension:

def capital_indexes():
    word =input("enter your word :")
    a = [i for i, j in enumerate(word) if j.isupper()]
    print(a)

capital_indexes()

Output (again with heLLo input):

[2, 3]
  • Related