找到大写字母

在这里插入图片描述

#解法一
def capitals(a):
    for index,s in enumerate(a):
        
        #isupper 如果字符串中都是大写,返回True,如果有一个小写,返回False
        
        if s.isupper():
            print(index)

capitals('anMIE')
#解法二
def capitals(word):
    capitals_index=[]
    for i in range(len(word)):
        if word[i].isupper():
            capitals_index.append(i)
            
    return capitals_index

print(capitals('CodEWaRs'))
#解法三
def capitals(word):
    return [i for i,s in enumerate(word) if 'A' <= s <= 'Z']
    
capitals('CodEWaRs')

猜你喜欢

转载自blog.csdn.net/weixin_42610407/article/details/87889151