Search code examples
pythonstringlowercase

Detect index of lower case letters


How do I get the index of all lower case letters of a string?

For example, the string "AbCd" would lead to [1,3].


Solution

  • A simple iteration over the string would do it:

    s = "AbCd"
    result = []
    for i, a in enumerate(s):
        if a.islower():
            result.append(i)
    

    Another way would be a list comprehension:

    s = "AbCd"
    result = [i for i, a in enumerate(s) if a.islower()]
    

    Both cases result in the same:

    result = [1,3]