- How to Check if String Contains Uppercase Letters in Python
- How to Check if String Contains Lowercase in Python
- Other Articles You’ll Also Like:
- About The Programming Expert
- Python String isupper()
- Introduction to the Python String isupper() method
- Python String isupper() method examples
- Summary
- Check if all uppercase python
- # Table of Contents
- # Check if a string contains any Uppercase letters in Python
- # Check if a string contains any Uppercase letters using a for loop
- # Check if a string contains any Uppercase letters using re.search()
- # Additional Resources
How to Check if String Contains Uppercase Letters in Python
In Python, we can check if a string contains uppercase characters by checking each letter to see if that letter is uppercase in a loop.
def checkStrContainsUpper(string): for x in string: if x == x.upper(): return True return False print(checkStrContainsUpper("all letters here are lowercase")) print(checkStrContainsUpper("We Have some uppercase Letters in this One.")) #Output: False True
When processing strings in a program, it can be useful to know if we have uppercase or lowercase characters. Using Python, we can easily check if string contains uppercase characters with the help of the Python upper() function.
To check if a string contains uppercase, we just need to loop over all letters in the string until we find a letter that is equal to that letter after applying the upper() function.
Below is a Python function which will check if a string contains uppercase characters.
def checkStrContainsUpper(string): for x in string: if x == x.upper(): return True return False print(checkStrContainsUpper("all letters here are lowercase")) print(checkStrContainsUpper("We Have some uppercase Letters in this One.")) #Output: False True
How to Check if String Contains Lowercase in Python
We can also check if a string contains lowercase characters in Python very easily.
To check if a string contains lowercase letters in Python, we can adjust our function that we defined above to use the Python lower() function, instead of the upper() function.
Below is a Python function which will check if a string contains lowercase characters.
def checkStrContainsLower(string): for x in string: if x == x.lower(): return True return False print(checkStrContainsLower("ALL THE LETTERS ARE UPPERCASE")) print(checkStrContainsLower("We Have some uppercase Letters in this One.")) #Output: False True
Hopefully this article has been useful for you to learn how to check if a string contains uppercase characters in Python.
Other Articles You’ll Also Like:
- 1. Get Size of File in Python with os.path.getsize() Function
- 2. How to Group By Columns and Find Sum in pandas DataFrame
- 3. Format Numbers as Dollars in Python with format()
- 4. pandas groupby size – Get Number of Elements after Grouping DataFrame
- 5. Python acos – Find Arccosine and Inverse Cosine of Number
- 6. Using Python to Check If a Number is a Perfect Square
- 7. Find Median of List in Python
- 8. Initialize Multiple Variables in Python
- 9. Using Python to Read Random Line from File
- 10. Using Python to Create List of Prime Numbers
About The Programming Expert
The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.
Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.
At the end of the day, we want to be able to just push a button and let the code do it’s magic.
You can read more about us on our about page.
Python String isupper()
Summary: in this tutorial, you’ll learn how to use the Python string isupper() method to check if all cases characters in a string are uppercase.
Introduction to the Python String isupper() method
Here is the syntax of the isupper() method:
str.isupper()
Code language: CSS (css)
The string isupper() method returns True if all cased characters in a string are uppercase. Otherwise, it returns False .
If the string doesn’t contain any cased characters, the isupper() method also returns False .
In Python, cased characters are the ones with general category property being one of:
Notice that to return a copy of the string with all cased characters converted to uppercase, you use the string upper() method.
Python String isupper() method examples
Let’s take some examples of using the string isupper() method.
The following example uses the isupper() to check if all cased characters of a string are uppercase:
message = 'PYTHON' is_uppercase = message.isupper() print(is_uppercase)
Code language: PHP (php)
True
Code language: PHP (php)
However, the following example returns False because the string contains some characters in lowercase:
language = 'Python' is_uppercase = language.isupper() print(is_uppercase)
Code language: PHP (php)
False
Code language: PHP (php)
The following example also returns False because the string doesn’t has any cased characters:
amount = '$100' is_uppercase = amount.isupper() print(is_uppercase)
Code language: PHP (php)
False
Code language: PHP (php)
Summary
- Use the Python string isupper() method to check if all cased characters of a string are uppercase.
Check if all uppercase python
Last updated: Jan 25, 2023
Reading time · 3 min
# Table of Contents
# Check if a string contains any Uppercase letters in Python
To check if a string contains any uppercase letters:
- Use a generator expression to iterate over the string.
- Use the str.isupper() method to check if each character is uppercase.
- If the condition is met for any of the characters, the string contains uppercase letters.
Copied!my_str = 'Bobby' contains_uppercase = any(char.isupper() for char in my_str) print(contains_uppercase) # 👉️ True if contains_uppercase: # 👇️ this runs print('The string contains uppercase letters') else: print('The string does NOT contain any uppercase letters') # -------------------------------------------------- # ✅ Extract the uppercase letters from a string my_str = 'BOBBYhadz123' only_upper = ''.join(char for char in my_str if char.isupper()) print(only_upper) # 👉️ BOBBY only_upper = [char for char in my_str if char.isupper()] print(only_upper) # 👉️ ['B', 'O', 'B', 'B', 'Y']
We used a generator expression to iterate over the string.
Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.
On each iteration, we use the str.isupper() method to check if the current character is an uppercase letter.
The str.isupper method returns True if all cased characters in the string are uppercase and the string contains at least one cased character, otherwise False is returned.
Copied!my_str = 'BOBBYHADZ.COM' all_uppercase = my_str.isupper() print(all_uppercase) # 👉️ True
The any function takes an iterable as an argument and returns True if any element in the iterable is truthy.
Copied!my_str = 'Bobby' contains_uppercase = any(char.isupper() for char in my_str) print(contains_uppercase) # 👉️ True if contains_uppercase: # 👇️ this runs print('The string contains uppercase letters') else: print('The string does NOT contain any uppercase letters')
If the condition is met for any of the characters in the string, the any() function short-circuits and returns True .
Alternatively, you can use a for loop.
# Check if a string contains any Uppercase letters using a for loop
This is a three-step process:
- Use a for loop to iterate over the string.
- Use the str.isupper() method to check if each character is an uppercase letter.
- If the condition is met, set a variable to True and break out of the loop.
Copied!contains_uppercase = False my_str = 'Bobby' for char in my_str: if char.isupper(): contains_uppercase = True break print(contains_uppercase) # 👉️ True
We used a for loop to iterate over the string.
On each iteration, we check if the current character is an uppercase letter.
If the condition is met, we set the contains_uppercase variable to True and break out of the for loop.
The break statement breaks out of the innermost enclosing for or while loop.
# Check if a string contains any Uppercase letters using re.search()
Alternatively, you can use the re.search() method.
The re.search() method will return a match object if the string contains uppercase letters, otherwise, None is returned.
Copied!import re def contains_uppercase(string): return bool(re.search(r'[A-Z]', string)) if contains_uppercase('Bobby'): # 👇️ this runs print('The string contains uppercase letters') else: print('The string does NOT contain any uppercase letters') print(contains_uppercase('A b c')) # 👉️ True print(contains_uppercase('abc 123')) # 👉️ False print(contains_uppercase('')) # 👉️ False print(contains_uppercase(' ')) # 👉️ False
The re.search method looks for the first location in the string where the provided regular expression produces a match.
The first argument we passed to the re.search() method is a regular expression.
The square brackets [] are used to indicate a set of characters.
We used the bool() class to convert the match object to True or the None value to False .
# Additional Resources
You can learn more about the related topics by checking out the following tutorials:
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.