How to Print only the Uppercase letters in the String Python

Print only the uppercase letters in the string python | Uppercase letters are also known as capital letters. Uppercase letters signal to the reader that something is important or significant. English alphabet uppercase letters: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z.

This python program using the built-in function and For loop to print uppercase characters. The isupper() function is used to check if the string contains any uppercase characters.

# Python program to print uppercase letters in the string

# take input
string = input('Enter any string: ')

upper = ''
for char in string:
    #check uppercase characters
    if char.isupper():
        upper += char

# print uppercase characters
print('Uppercase characters:', upper)

Output for the different input values:-

Enter any string: Know Program
Uppercase characters: KP

Enter any string: UPPERcase CHARacters
Uppercase characters: UPPERCHAR

Enter any string: PytHon PRoGRam
Uppercase characters: PHPRGR

We can also write this program in a simple way to print only the uppercase letters in the string in Python 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 check if the string contains any uppercase characters.

string = input('Enter any string: ')
upper = [char for char in string if char.isupper()]
print('Uppercase characters:', upper)

Extract Uppercase words in Python using filter() and lambda

In this program, We used the filter function along with lambda functionality. Also, we used isupper() function to check if the string contains any uppercase characters. The filter function performs the specific selection of case characters and the lambda function is used for string traversal.

# Python program to print uppercase letters in the string

# take input
string = input('Enter any string: ')

# extract uppercase characters using filter() and lambda
upper = list(filter(lambda x: x.isupper(), string))

# print uppercase characters
print('Uppercase characters:', upper)

Output:-

Enter any string: Using Filter and Lambda
Uppercase characters: [‘U’, ‘F’, ‘L’]

Python Check if String Contains Uppercase using join()

In the above program, We used filter() and lambda but in this program, We are using join() methods to print all uppercase characters. The join() method takes all items in an iterable and joins them into one string. A string must be specified as the separator.

# Python program to print uppercase letters in the string

# take input
string = input('Enter any string: ')

# extract uppercase characters using join()
upper = (''.join(x for x in string if x.isupper()))

# print uppercase characters
print('Uppercase characters:', upper)

Output:-

Enter any string: JoIn MetHOds
Uppercase characters: JIMHO

Also See:- Python program to convert uppercase to lowercase

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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