This article will discuss different ways to count the number of uppercase characters in a string.
Table of Contents
- Count uppercase characters in a python string using for-loop
- Count uppercase characters in a python string using sum() function
- Count uppercase characters in a python string using regex
- Count uppercase characters in a python string using list comprehension
Count uppercase characters in a python string using for-loop
We can iterate over a string character by character using a for loop and count number of uppercase characters in the string during iteration,
def count_upper_case_letters(str_obj): count = 0 for elem in str_obj: if elem.isupper(): count += 1 return count count = count_upper_case_letters('This is a Sample Text') print(count)
Output:
3
Count uppercase characters in a python string using sum() function
We can iterate over all the characters in a string using generator expression. When an uppercase character is found during iteration, yield that to the sum() function. In the end sum() function will return the total number of uppercase characters in a string,
str_obj = 'This is a Sample Text' count = sum(1 for elem in str_obj if elem.isupper()) print(count)
Output:
3
Count uppercase characters in a python string using regex
We can call the findall() method of the regex module in Python with a pattern that matches all the uppercase characters in the string. findall() will return a list of all matches in the string, which in our case will be the upper case characters. Then, by fetching that uppercase character list’s size, we can get a count of uppercase characters in the string. For example,
import re str_obj = 'This is a Sample Text' count = len(re.findall(r'[A-Z]',str_obj)) print(count)
Output:
3
Count uppercase characters in a python string using list comprehension
Using list comprehension, iterate over all the characters in a string, and create a list of only uppercase characters from the string. Then, by fetching that uppercase character list’s size, we can get a count of uppercase characters in the string. For example,
str_obj = 'This is a Sample Text' count = len([elem for elem in str_obj if elem.isupper()]) print(count)
Output:
3
Summary
In this article, we discussed different ways to count upper case characters in a string.
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.