Open In App

Python – Get the indices of Uppercase characters in given string

Last Updated : 03 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a String extract indices of uppercase characters.

Input : test_str = ‘GeeKsFoRGeeks’ 

Output : [0, 3, 5, 7, 8] 

Explanation : Returns indices of uppercase characters. 

Input : test_str = ‘GFG’ 

Output : [0, 1, 2] 

Explanation : All are uppercase.

Method #1 : Using list comprehension  + range() + isupper()

In this, we iterate through the indices till string length, and check for uppercase character using isupper(), if found, index is recorded.

Python3




# Python3 code to demonstrate working of
# Uppercase Indices
# Using list comprehension + range() + isupper()
 
# initializing string
test_str = 'GeeKsFoRGEEks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# Uppercase check using isupper()
res = [idx for idx in range(len(test_str)) if test_str[idx].isupper()]
 
# printing result
print("Uppercase elements indices : " + str(res))


Output

The original string is : GeeKsFoRGEEks
Uppercase elements indices : [0, 3, 5, 7, 8, 9, 10]

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #2 : Using enumerate() + isupper()

In this, the indices are captured using enumerate(), and isupper() does task of uppercase check as in above method.

Python3




# Python3 code to demonstrate working of
# Uppercase Indices
# Using enumerate() + isupper()
 
# initializing string
test_str = 'GeeKsFoRGEEks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# Uppercase check using isupper()
# enumerate() gets indices
res = [idx for idx, chr in enumerate(test_str) if chr.isupper()]
 
# printing result
print("Uppercase elements indices : " + str(res))


Output

The original string is : GeeKsFoRGEEks
Uppercase elements indices : [0, 3, 5, 7, 8, 9, 10]

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #3 : Without isupper() method

Python3




# Python3 code to demonstrate working of
# Uppercase Indices
# Using list comprehension + range() + isupper()
 
# initializing string
test_str = 'GeeKsFoRGEEks'
upperalphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
# printing original string
print("The original string is : " + str(test_str))
res=[]
# Uppercase check
for i in range(0,len(test_str)):
    if test_str[i] in upperalphabets:
        res.append(i)
# printing result
print("Uppercase elements indices : " + str(res))


Output

The original string is : GeeKsFoRGEEks
Uppercase elements indices : [0, 3, 5, 7, 8, 9, 10]

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #4 : Using ord() function 

Python3




# Python3 code to demonstrate working of
# Uppercase Indices
 
# initializing string
test_str = 'GeeKsFoRGEEks'
 
# printing original string
print("The original string is : " + str(test_str))
res=[]
# Uppercase check
for i in range(0,len(test_str)):
    if ord(test_str[i]) in range(65,91):
        res.append(i)
# printing result
print("Uppercase elements indices : " + str(res))


Output

The original string is : GeeKsFoRGEEks
Uppercase elements indices : [0, 3, 5, 7, 8, 9, 10]

The Time and Space Complexity for all the methods are the same:

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #4 : Using operator.countOf() method

Python3




# Python3 code to demonstrate working of
# Uppercase Indices
# Using operator.countOf() method
import operator as op
 
# initializing string
test_str = 'GeeKsFoRGEEks'
upperalphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
# printing original string
print("The original string is : " + str(test_str))
res = []
# Uppercase check
for i in range(0, len(test_str)):
    if op.countOf(upperalphabets, test_str[i]) > 0:
        res.append(i)
# printing result
print("Uppercase elements indices : " + str(res))


Output

The original string is : GeeKsFoRGEEks
Uppercase elements indices : [0, 3, 5, 7, 8, 9, 10]

Time Complexity: O(n)

Auxiliary Space: O(n)

Method#5: Using Recursive method.

Python3




# Python3 code to demonstrate working of
# Uppercase Indices
# Using recursive method
def uppercase_indices(string, idx=0, result=None):
    if result is None:
        result = []
    if idx == len(string):
        return result
    if string[idx].isupper():
        result.append(idx)
    return uppercase_indices(string, idx + 1, result)
# initializing string
test_str = 'GeeKsFoRGEEks'
 
# printing original string
print("The original string is : " + str(test_str))
 
res=uppercase_indices(test_str)
 
# printing result
print("Uppercase elements indices : " + str(res))
#this code contributed by tvsk


Output

The original string is : GeeKsFoRGEEks
Uppercase elements indices : [0, 3, 5, 7, 8, 9, 10]

Time Complexity: O(n)

Auxiliary Space: O(n)

Method#6: Using re and finditer function

Python3




import re
 
# initializing string
test_str = 'GeeKsFoRGEEks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# Finding indices of uppercase characters using re.finditer
res = [match.start() for match in re.finditer(r'[A-Z]', test_str)]
 
# printing result
print("Uppercase elements indices : " + str(res))
#This code is contributed by Vinay Pinjala.


Output

The original string is : GeeKsFoRGEEks
Uppercase elements indices : [0, 3, 5, 7, 8, 9, 10]

Time Complexity: O(n)

Auxiliary Space: O(n)

Method#7:numpy():

Algorithm:

1.Convert the given string into a list of characters using the list() method.
2.Create a numpy array from the list of characters using the np.array() method.
3.Use the np.char.isupper() method to check whether each character is uppercase or not.
4.Convert the numpy array to a list using the tolist() method.
5.Use the np.where() method to find the indices where the characters are uppercase.
6.Convert the indices to a list and return the list.

Python3




import numpy as np
 
test_str = 'GeeKsFoRGEEks'
# printing original string
print("The original string is : " + str(test_str))
arr = np.array(list(test_str))
res = list(np.where(np.char.isupper(arr.tolist()))[0])
print("Uppercase elements indices : " + str(res))
#This code is contributed by Jyothi pinjala


Output:

The original string is : GeeKsFoRGEEks
Uppercase elements indices : [0, 3, 5, 7, 8, 9, 10]

Time complexity: O(n)
The time complexity of the code is O(n) because it involves converting the string to a list, creating a numpy array, and then checking whether each character in the array is uppercase or not. All these operations take O(n) time.

Space complexity: O(n)
The space complexity of the code is O(n) because it involves creating a list of characters from the string, creating a numpy array from the list, and then creating a list from the numpy array. All these operations require O(n) space.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads