How to extract uppercase words in string Python

In this post, we will learn how to extract uppercase words in string Python with examples. We have used Regular expression to extract capital words from a given string.” A regular expression (or RE) specifies a set of strings that matches it; the functions in this module let you check if a particular string matches a given regular expression”.We will cover extracting uppercase words from lists in Python, extracting alphanumeric uppercase words from strings in Python, Extract Only uppercase letters from strings in Python, Count uppercase letters from strings in Python

How to extract uppercase words in string Python


In this example, we have extracted all uppercase words from Python string using Regular expression. In such a case first, we have to import the Regular expression module in our program using “import re”.The regular expression is as follows. The re module findall() function will return the word that matches the given pattern.

  • [A-Z]: It extracts capital letters between A-Z.
  • \b: Start of word
import re
strText = 'Welcome to DEVENUM.COM,LET FIND UPPER CASE STRING';
print(re.findall(r'\b[A-Z]+(?:\s+[A-Z]+)*\b', strText))

Output

['DEVENUM', 'COM', 'LET FIND UPPER CASE STRING']

2. How to extract uppercase words in string Python using Regex


In this example, We are using the Regular expression ‘[A-Z]+’ to extract the uppercase letter and upper words from a string. Let us understand code and output.

import re
myStr = "Welcome folks'S TO DEVENUM! HOW ARE YOU"
pattern =  '[A-Z]+'
print(re.findall(pattern, myStr))

Output

[' TO DEVENUM', ' HOW ARE YOU']

3. Extract uppercase words from list in Python


To extract uppercase words from a list in Python. We have iterated over the list using for loop and on each iteration, we have extracted uppercase words using the Regular expression using the re.findall() function and display the result.

import re
mylist = ['Welcome','to','DEVENUM','COM','LET', 'FIND','CASE']
for item in mylist: 
 print(re.findall(r'\b[A-Z]+(?:\s+[A-Z]+)*\b', item))

Output

[]
[]
['DEVENUM']
['COM']
['LET']
['FIND']
['CASE']

4. Extract uppercase alphanumeric words of string in Python


We have used alphanumeric words from a string.We have used Regular expression [A-Z0-9][A-Z0-9]+|\b[A-Z] where 0-9 used to include digit while matching the string.

import re
myStr = "Welcome D2'S TO D56EV ENUM! H69OW A89E Y20OU"
pattern =  '[A-Z0-9][A-Z0-9]+|\b[A-Z]*\b'
print(re.findall(pattern, myStr))

Output

['D2', 'TO', 'D56EV', 'ENUM', 'H69OW', 'A89E', 'Y20OU']

5. Extract Only uppercase letters from string Python


To find all the upper case letters from a string. We have used the filter function along with lambda and checked if the character in a string is uppercase using isupper() method. To return a list we have converted the result to a list using the list() method.

strText = 'Welcome to DEVENUM.COM';


def Is_upper_Letter(myStr):
    return list(filter(lambda char: char.isupper(), myStr))

print(Is_upper_Letter(strText))

Output

['W', 'D', 'E', 'V', 'E', 'N', 'U', 'M', 'C', 'O', 'M']

6. Count uppercase letters in string Python


To find the count of uppercase letters in a given string. We have iterated over each character of the string by using generator expression. When the upper case character is found, return the character to the sum function and when iteration is over, the sum function will return the count of all upper case letters in the string.

strText = 'Welcome to DEVENUM.COM';
UpperCase_count = sum(1 for item in strText if item.isupper())
print('Upper case letter in String :',UpperCase_count)

Output

Upper case letter in String :11

Summary

In this post, we have learned How to extract uppercase words in string Python by using regular expressions and findall() function of the re module. We have also covered, extracting uppercase words from lists in Python, extracting alphanumeric uppercase words from strings in Python, Extract Only uppercase letters from strings in Python, Count uppercase letters from strings in Python