Python Interview Questions - Python program to identify index position of all uppercase letters in a string.

 


Question:
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].

code:
def capital_indexes(string_input):
    Capital_index_list = []
    for i in enumerate(string_input):
       if i[1].isupper():         
           Capital_index_list.append(i[0])
    return Capital_index_list

string_input = "HeLlO"
output_list =  capital_indexes(string_input)
print(output_list)

Points to Note:

1) How to check capital :  item.isupper() method
2) Enumerate method :  enumerate returns a tuple with first element as index and second element as actual item.

Output:



Comments