In this tutorial, we will look at how to check if a string in Python contains any uppercase characters or not with the help of some examples.
How to check if a string contains any uppercase characters?
You can use a combination of the Python built-in any()
function and the string isupper()
function to check if a Python string contains any uppercase characters or not.
The built-in any()
function in Python takes an iterable as an argument and returns True
if any value in the iterable is truthy (evaluates to True
in a boolean context).
So, for each character in the string, check if it is uppercase or not using the string isupper()
function and pass the resulting boolean iterable as an argument to the any()
function. The following is the syntax –
# check if string contains any uppercase characters any(ch.isupper() for ch in s)
It returns True
if any (that is, at least one) character in the given string is in uppercase.
Examples
Let’s now look at some examples. First, we will create some strings that we will be using throughout this tutorial.
# create strings s1 = "Avengers" s2 = "avengers" s3 = "" # display the strings print(s1) print(s2) print(s3)
Output:
Avengers avengers
Here, we created three strings – s1
, s2
, and s3
. The string s1
contains an uppercase character, the string s2
has only lowercase characters, and the string s3
is an empty string.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Let’s use a list comprehension to check if each character in a string is uppercase or not and pass the resulting boolean list as an argument to the any()
function.
# check if string contains any uppercase characters print(any([ch.isupper() for ch in s1])) print(any([ch.isupper() for ch in s2])) print(any([ch.isupper() for ch in s3]))
Output:
True False False
We get True
for s1
as it does contain at least one uppercase letter. We get False
for s2
as it does not contain any uppercase letters. Note that we get False
for an empty string.
The any()
function takes an iterable as an argument, you can also directly pass an iterator which results in an iterable to the any()
function (and avoid using the list comprehension).
# check if string contains any uppercase characters print(any(ch.isupper() for ch in s1)) print(any(ch.isupper() for ch in s2)) print(any(ch.isupper() for ch in s3))
Output:
True False False
We get the same results as above.
Now, in case you want to check whether the entire string is in uppercase or not, use the string isupper()
function directly on the string.
For example,
# check if all characters in string are uppercase print("AVENGERS".isupper()) print("Avengers".isupper())
Output:
True False
We get True
for the string “AVENGERS” and False
for the string “Avengers”.
You might also be interested in –
- Python String Uppercase – With Examples
- Python String Lowercase – With Examples
- Python – Capitalize First Letter of Each Word
- Python – Check If All Characters in String are Same
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.