Check if First Letter of String is Uppercase in Python

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()

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:

Advertisements
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

   

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.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top