Detect capital letters in a string in Python

In this tutorial we will learn how to write a program to create random password of n characters in python with some coding example. To detect capital letters in a string in Python, you can use various methods. Here we Detect Capital Letters in a String Using List Comprehension method.Python’s string isupper() function is used to determine the presence of all uppercase alphabets in a string.The list comprehension is primarily used to iterate over the list and isupper function checks for the uppercase characters.

Detect capital letters in a string in Python

Let’s learn this with a easy coding examples,
Step 1: Function Definition
Defines a function named ‘detect_capital_letters’ that takes a single parameter ‘s’. Here ‘s’ represents the input string in which the function will search for capital letters.

def detect_capital_letters(s):

Step 2: List Comprehension
Returns a list of capital letters found in the string ‘s’. List Comprehension [char for char in s if char.isupper()]

  • Iteration: for char in s iterates through each character in the string s.
  • Condition: if char.isupper() checks if the character char is an uppercase letter.
  • Result: Includes only those characters that are uppercase in the resulting list.
return [char for char in s if char.isupper()]

Step 3: User Input
Enter a string and stores the input in the variable ‘user_input’.
‘input(“Enter a string”)’ displays a prompt message and waits for the user to type a string. The entered string is assigned to ‘user_input’.

user_input = input("Enter a string: ")

Step 4: Function Call
detect_capital_letters(user_input)processes the user input to find capital letters. The result, which is a list of capital letters, is stored in the variable capital_letters.

capital_letters = detect_capital_letters(user_input)

Step 5: Conditional Check and Output
Checks if the capital_letters list is not empty.
if capital_letters: evaluates to True if the list contains any elements (i.e., if there are any capital letters).
Prints the list of capital letters found in the string.
"Capital letters found in the string:" is the message that will be printed.
', '.join(capital_letters) converts the list of capital letters into a comma-separated string.

if capital_letters:
     print("Capital letters found in the string:", ', '.join(capital_letters))

Defines the block of code to be executed if the capital_letters list is empty.
Prints a message indicating that no capital letters were found.
This message is displayed when the list capital_letters is empty, meaning no uppercase letters were detected in the user input.

else:
    print("No capital letters found in the string.")
The final code is:
def detect_capital_letters(s):
    return [char for char in s if char.isupper()]

# Example usage
user_input = input("Enter a string: ")
capital_letters = detect_capital_letters(user_input)

if capital_letters:
    print("Capital letters found in the string:", ', '.join(capital_letters))
else:
    print("No capital letters found in the string.")
Execution of the code:
Enter a string: HGCHCJVjgigcskuhdsa
Capital letters found in the string: H, G, C, H, C, J, V
Summary:

List comprehension is a preferred technique for detecting capital letters in a string due to its conciseness, readability, and efficiency, making it a valuable tool for cleaner and more effective Python code.

 

 You may also learn,

Leave a Comment

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

Scroll to Top