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:
Frequently Asked:
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.