Extract all the capital letters from a text file using Python

In this tutorial, we are working with text files, where you might encounter scenarios where you must extract specific characters, such as all the capital letters. This can be particularly useful for tasks like data analysis, text processing, or even creating fun puzzles!

Text File as Text.txt:

Rajni is the best-friend of Kusha and Manali is a sister of Uday

Method 1: Using regular expressions

import re
with open('Text.txt', 'r') as file:
    all_Text = file.read()
upper_letters = re.findall(r'[A-Z]', all_Text) 
print("All Capital letters Using Regular expression :" + str(upper_letters))

Break-down of the Code:

  • The code uses Python’s ‘re’ module to perform regular expression operations. Regular expressions are a powerful tool for matching patterns in strings, making it easy to find specific types of characters.
  • The pattern r'[A-Z]’ finds all uppercase letters in the string. This pattern matches any uppercase letter from ‘A’ to ‘Z’, enabling efficient and concisely extraction of uppercase characters.

Output:

All Capital letters Using Regular expression :['R', 'K', 'M', 'U']

Method 2: Using isupper() 

with open('Text.txt', 'r') as file:
    all_Text = file.read()
upper_letters = [char for char in all_Text if char.isupper()] 
print("All Capital letters Using isupper() method :"+ str(upper_letters))

Break-down of the code:

  • The  isupper() method is used to check whether each character in the string is uppercase. It returns True if the character is uppercase, and False otherwise.

Output:

All Capital letters Using isupper() method :['R', 'K', 'M', 'U']

Method 3: Using filter() function

with open('Text.txt', 'r') as file:
    all_Text = file.read()
upper_letters = list(filter(lambda c: c.isupper(), all_Text))
print("All Capital letters Using filter :" + str(upper_letters))

Break-down of the code:

  • The ‘filter()’ function is used to construct an iterator to filter out elements for which a function returns True. In this case, it filters out all characters in the string that are uppercase.
  •  A lambda function is an anonymous function defined with the lambda keyword. Here, ‘lambda c: c.isupper()’ is used to check if each character c in the string is uppercase.

Output:

All Capital letters Using filter :['R', 'K', 'M', 'U']

Method 4: Using ASCII values

with open('Text.txt', 'r') as file:
    all_Text = file.read()
upper_letters = [char for char in all_Text if 65 <= ord(char) <= 90]
print("All Capital letters Using ASCII Values :" + str(upper_letters))

Break-down of the code:

  • Here , ‘ord()’ function returns the Unicode code point of a given character. In this case, it is used to get the numeric value of each character in the string.
  • The code uses the ASCII values for uppercase letters (65 to 90) to filter out the uppercase characters. This approach explicitly checks if the character’s ASCII value falls within the range of uppercase letters.

Output:

All Capital letters Using ASCII Values :['R', 'K', 'M', 'U']

Method 5:: Without using built-in methods

with open('Text.txt', 'r') as file:
    all_Text = file.read()
upperalpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
upper_letters = []
for i in all_Text:
    if i in upperalpha:
        upper_letters.append(i)
print("All Capital letters :" + str(upper_letters))

Break-down of the code:

  • Here, predefined string upper alpha containing all uppercase letters from ‘A’ to ‘Z’. This makes it easy to check if a character is uppercase by seeing if it exists in this string.

Output:

All Capital letters :['R', 'K', 'M', 'U']

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top