Python – Extract Upper Case Characters

Sometimes, while working with strings, we are concerned about the case-sensitivity of strings and might require to get just a specific case of character in a long string. Let’s discuss certain ways in which only uppercase letters can be extracted from a string.

Method #1 : Using list comprehension + isupper()
List comprehension and isupper function can be used to perform this particular task. The list comprehension is primarily used to iterate over the list and isupper function checks for the uppercase characters.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Extract Upper Case Characters 
# Using list comprehension + isupper()
  
# initializing string 
test_str = "GeeksForGeeKs"
  
# printing original string 
print("The original string is : " + str(test_str))
  
# Extract Upper Case Characters 
# Using list comprehension + isupper()
res = [char for char in test_str if char.isupper()]
  
# printing result 
print("The uppercase characters in string are : " + str(res))

chevron_right


Output :

The original string is : GeeksForGeeKs
The uppercase characters in string are : ['G', 'F', 'G', 'K']

 

Method #2 : Using filter() + lambda
Filter function along with lambda functionality can be used to perform this particular task. The filter function performs the specific selection of case characters and lambda function is used for string traversal.



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Extract Upper Case Characters
# Using filter() + lambda
  
# initializing string 
test_str = "GeeksForGeeKs"
  
# printing original string 
print("The original string is : " + str(test_str))
  
# Extract Upper Case Characters
# Using filter() + lambda
res = list(filter(lambda c: c.isupper(), test_str))
  
# printing result 
print("The uppercase characters in string are : " + str(res))

chevron_right


Output :

The original string is : GeeksForGeeKs
The uppercase characters in string are : ['G', 'F', 'G', 'K']

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.