Python – Get the indices of Uppercase characters in given string
Last Updated :
03 Mar, 2023
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
test_str = 'GeeKsFoRGEEks'
print ( "The original string is : " + str (test_str))
res = [idx for idx in range ( len (test_str)) if test_str[idx].isupper()]
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
test_str = 'GeeKsFoRGEEks'
print ( "The original string is : " + str (test_str))
res = [idx for idx, chr in enumerate (test_str) if chr .isupper()]
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
test_str = 'GeeKsFoRGEEks'
upperalphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
print ( "The original string is : " + str (test_str))
res = []
for i in range ( 0 , len (test_str)):
if test_str[i] in upperalphabets:
res.append(i)
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
test_str = 'GeeKsFoRGEEks'
print ( "The original string is : " + str (test_str))
res = []
for i in range ( 0 , len (test_str)):
if ord (test_str[i]) in range ( 65 , 91 ):
res.append(i)
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
import operator as op
test_str = 'GeeKsFoRGEEks'
upperalphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
print ( "The original string is : " + str (test_str))
res = []
for i in range ( 0 , len (test_str)):
if op.countOf(upperalphabets, test_str[i]) > 0 :
res.append(i)
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
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)
test_str = 'GeeKsFoRGEEks'
print ( "The original string is : " + str (test_str))
res = uppercase_indices(test_str)
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#6: Using re and finditer function
Python3
import re
test_str = 'GeeKsFoRGEEks'
print ( "The original string is : " + str (test_str))
res = [match.start() for match in re.finditer(r '[A-Z]' , test_str)]
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#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'
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))
|
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
Please Login to comment...