This article will discuss how to check if a string starts with a capital letter in Python.
Table of Contents
- Check if First Letter of String is Uppercase using isupper()
- Check if the First Letter of String is Uppercase using Regex
Check if First Letter of String is Uppercase using isupper()
The isupper() function returns True if all the string characters are upper case characters. But we are interested in the first letter of the string only. Therefore, we will select the first character of string using subscript operator i.e. str[0], and call the isupper() on it to check if the first character is uppercase or not. Let’s understand this with some examples,
Example 1:
sample_str = "The version" # Check if string starts with an uppercase letter if sample_str[0].isupper(): print("The String '%s' starts with a Capital/Uppercase Letter" % (sample_str)) else: print("First letter of String '%s' is not Upper case" % (sample_str))
Output:
The String 'The version' starts with a Capital/Uppercase Letter
In the string, the first letter was in uppercase.
Example 2:
sample_str = "last version" # Check if string starts with an uppercase letter if sample_str[0].isupper(): print("The String '%s' starts with a Capital/Uppercase Letter" % (sample_str)) else: print("First letter of String '%s' is not Upper case" % (sample_str))
Output:
First letter of String 'last version' is not Upper case
In the string, the first letter was not the uppercase letter.
Check if the First Letter of String is Uppercase using Regex
In Python, the regex module provides a function search(). It accepts a regex pattern and string as arguments. It looks for a given regex pattern in the given string. If a match to the pattern is found, it returns a Match object; otherwise, it returns None. We can use this search() function to check if a string starts with an uppercase letter by using the regex pattern “^[A-Z]”.
The caret ^ has a special meaning in a regex. It is called the “anchor”. The caret ^ matches at the beginning of the text. By using [A-Z] after caret ^ i.e. “^[A-Z]”, we are specifying that string must start with a letter A to Z i.e. an uppercase letter.
Let’s see some examples,
Example 1:
import re sample_str = "The version" # Check if string starts with an uppercase letter if re.search("^[A-Z]", sample_str) is not None: print("The String '%s' starts with a Capital/Uppercase Letter" % (sample_str)) else: print("First letter of String '%s' is not Upper case" % (sample_str))
Output:
The String 'The version' starts with a Capital/Uppercase Letter
Here we used a string that started with an uppercase letter character.
Example 2:
import re sample_str = "last version" # Check if string starts with an uppercase letter if re.search("^[A-Z]", sample_str) is not None: print("The String '%s' starts with a Capital/Uppercase Letter" % (sample_str)) else: print("First letter of String '%s' is not Upper case" % (sample_str))
Output:
First letter of String 'last version' is not Upper case
It was a negative test. Here we used a string that starts with a lowercase character in Python.
Summary
We learned two different ways to check if a string starts with an uppercase/capital letter.
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.