Get the indices of Uppercase Characters in a given String using Python


In Python, we can get the indices of uppercase characters in a given string using methods like using List Comprehension, using a For Loop, using Regular Expressions etc. Finding the indices of Uppercase characters can be useful in text analysis and manipulation. In this article, we will explore different methods to obtain the indices of uppercase characters in a given string.

Method 1:Using List Comprehension

Using list comprehension we can iterate through the characters of a string, check if they are uppercase using the isupper() method, and stores their indices in a list. It provides a concise and efficient way to achieve the desired result.

Algorithm

  • Initialize an empty list to store the indices.

  • Iterate through each character in the string using enumerate().

  • Check if the character is uppercase using isupper().

  • If it is uppercase, append its index to the list.

  • Print the list of indices.

Syntax

indices = [index for index, char in enumerate(string) if char.isupper()]

Here, we utilize a list comprehension to iterate through the string characters and obtain their indices if they are uppercase. The enumerate() function is used to retrieve both the index and the character from the string.

Example

In the below example, the string "Hello World" contains two uppercase characters: 'H' and 'W'. By applying the list comprehension, we iterate through each character in the string and check if it is uppercase using the isupper() method. If it is, we store its index in the indices list. Finally, we print the list of indices.

string = "Hello World"
indices = [index for index, char in enumerate(string) if char.isupper()]
print(indices)

Output

[0, 6]

Method 2:Using a For Loop

By combining a for loop with the enumerate() function, this method iterates through the characters of a string, checks if they are uppercase using isupper(), and appends their indices to a list. It provides an easily understandable approach to obtain the indices of uppercase characters.

Algorithm

  • Initialize an empty list to store the indices

  • Iterate through each character in the string using a for loop and enumerate().

  • Check if the character is uppercase using isupper().

  • If it is uppercase, append its index to the list.

  • Print the list of indices.

Syntax

indices = []
for index, char in enumerate(string):
    if char.isupper():
        indices.append(index)

Here, we utilize a for loop combined with the enumerate() function to iterate through the string characters. We check if each character is uppercase using isupper() and, if so, append its index to the indices list.

Example

In the below example, the string "Python Is Great" contains three uppercase characters: 'P', 'I', and 'G'. By utilizing a for loop, we iterate through each character in the string. If the character is uppercase, we append its index to the indices list. Finally, we print the list of indices.

string = "Python Is Great"
indices = []
for index, char in enumerate(string):
    if char.isupper():
        indices.append(index)
print(indices)

Output

[0, 7, 10]

Method 3:Using Regular Expressions (re module)

This method utilizes the re module in Python, allowing for pattern−based searching. By using the re.finditer() function with the regular expression pattern [A−Z], it identifies uppercase characters in a string and retrieves their indices using the start() method.

Algorithm

  • Import the re module.

  • Use re.finditer() to find all matches of uppercase characters using the regular expression pattern [A−Z].

  • Iterate through the match objects returned by re.finditer().

  • Use the start() method of each match object to obtain the index of the match

  • Append each index to the list of indices

  • Print the list of indices.

Syntax

import re
indices = [m.start() for m in re.finditer(r'[A-Z]', string)]

Here, we import the re module, which provides support for regular expressions. We use the finditer() function to search for uppercase characters in the string using the regular expression pattern [A−Z]. The start() method of the returned match object is used to obtain the index of each match.

Example

In the below example, the string "OpenAI's ChatGPT is Amazing" contains four uppercase characters: 'O', 'A', 'C', and 'G'. By utilizing the re.finditer() function with the regular expression pattern [A−Z], we find all the matches and obtain their indices using the start() method. Finally, we print the list of indices.

import re
string = "OpenAI's ChatGPT is Amazing"
indices = [m.start() for m in re.finditer(r'[A-Z]', string)]
print(indices)

Output

[0, 4, 5, 9, 13, 14, 15, 20]

Conclusion

In this article, we discussed how we can get the indices of uppercase characters in a given string using different ways in Python. We discussed three methods: list comprehension, for loop, and regular expressions. Each method offers a unique approach, allowing developers to choose the one that best suits their needs.

Updated on: 17-Jul-2023

936 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements