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 string value to a literal.

A string in Python is represented by a string class which provides several functions and methods using which you can perform various operations on strings.

In this article, we are going to find out how to test if a string starts with a capital letter using Python.

Using isupper() method

One way to achieve this is using the inbuilt string method isupper(). We should access the first letter of the string using indexing and then send the character to isupper() method, this method returns True if the given character is Capital otherwise, it returns False.

Example 1

In the example given below, we are taking a string as input and we are checking if the first letter is capital or not using the isupper() method.

str1 = "Welcome to Tutorialspoint" print("The given string is") print(str1) print("Checking if the first character is Capital or not") if (str1[0].isupper()): print("The first letter is a capital letter") else: print("The first letter is not a capital letter")

Output

The output of the above example is,

The given string is
Welcome to Tutorialspoint
Checking if the first character is Capital or not
The first letter is a capital letter

Example 2

In the example given below, we are taking the same program as above but we are taking different inputs and checking.

str1 = "welcome to Tutorialspoint" print("The given string is") print(str1) print("Checking if the first character is Capital or not") if (str1[0].isupper()): print("The first letter is a capital letter") else: print("The first letter is not a capital letter")

Output

The output of the above example is,

The given string is
welcome to Tutorialspoint
Checking if the first character is Capital or not
The first letter is not a capital letter

Using Regular Expressions

You can test if a string starts with a capital letter using Python using regular expressions. To use the re library, import it and install it if it isn't already installed.

We'll use the regular expression "substring" after importing the re library. With the specified regular expression, the re.search() function checks if the text starts with a capital letter.

Example 1

In the example given below, we are taking a string as an input and we are checking if the first letter of the string is capital or not using Regular Expressions.

import re str1 = "Welcome to Tutorialspoint" print("The given string is") print(str1) print("Checking if the first character is Capital or not") if re.search("^[A-Z]", str1): print("The first letter is a capital letter") else: print("The first letter is not a capital letter")

Output

The output of the above example is,

The given string is
Welcome to Tutorialspoint
Checking if the first character is Capital or not
The first letter is a capital letter

Example 2

In the example given below, we are taking the same program as above but we are taking different input and checking.

import re str1 = "welcome to Tutorialspoint" print("The given string is") print(str1) print("Checking if the first character is Capital or not") if re.search("^[A-Z]", str1): print("The first letter is a capital letter") else: print("The first letter is not a capital letter")

Output

The output of the above example is,

The given string is
welcome to Tutorialspoint
Checking if the first character is Capital or not
The first letter is not a capital letter

Updated on: 26-Oct-2022

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements