How can I test if a string starts with a capital letter using Python?



A string is a collection of characters that can represent a single word or a whole sentence. Unlike Java, there is no need to declare Python strings explicitly; we can directly assign a string value to a literal.

While working with the string, we will encounter situations to check whether the string starts with a capital letter. This can be useful for validating the names or sentences to ensure they follow certain formatting rules.

Python provides multiple methods to test this condition. In this article, we will explore how to test if a string with a capital letter.

Using Python str.isupper() Method

The Python isupper() method is used to check whether all the characters of the given string are in uppercase or not. It returns true if all the characters in the string are uppercase; otherwise, it returns false.

In this approach, we are going to consider str[0] to get the first character of the string and use the isupper() method, which returns true if the character is in uppercase.

Syntax

Following is the syntax of Python str.isupper() method -

str.isupper()

Example

Let's look at the following example, where we are going to check whether the first character is an uppercase letter or not by using the str[0].isupper().

str1 = "Welcome to Tutorialspoint"
if (str1[0].isupper()):
   print("The first letter is a capital letter")
else:
   print("The first letter is not a capital letter")

The output of the above program is as follows -

The first letter is a capital letter

Using Python str.startswith() Method

The second approach is by using the Python startswith() method, which is used to check whether the string starts with the specified prefix (can be a string or a tuple).

Here, we are passing the parameter as 'tuple(string.ascii_uppercase)' to create a tuple of uppercase characters and applying the startswith() to check if the string begins with any of these uppercase characters.

Syntax

Following is the syntax of Python str.startswith() method -

str.startswith(value, start, end)

Example

Consider the following example, where we are going to use the startswith() method to check whether the string starts with a capital letter.

import string
str1 = "TutorialsPoint"
if str1.startswith(tuple(string.ascii_uppercase)):
    print("Starts with a capital letter")
else:
    print("Does not starts with a capital letter")

The output of the above program is as follows -

Starts with a capital letter

Using the "re" Module

The Python "re" module supports regular expressions, helping in string pattern matching and manipulation. It includes functions like match(), search() to identify, extract, or replace patterns in strings.

In this approach, we are going to use the Python re.match() method along with the regex pattern '[A-Z]' to check if the first character is between 'A' and 'Z'.

Example

In the following example, we are going to use a regular expression to check if the first character is uppercase.

import re
str1 = "Welcome"
if re.match(r'[A-Z]', str1):
   print("Starts with a capital letter")
else:
   print("Does not start with a capital letter")

The output of the above program is as follows -

Starts with a capital letter
Updated on: 2025-05-20T12:57:53+05:30

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements