Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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. While working with strings, we often need to check whether the string starts with a capital letter. This can be useful for validating names or sentences to ensure they follow certain formatting rules.
Python provides multiple methods to test if a string starts with a capital letter. In this article, we will explore three different approaches to accomplish this task.
Using str.isupper() Method
The isupper() method checks whether all characters in a string are uppercase. To test if a string starts with a capital letter, we can use str[0].isupper() to check just the first character.
Syntax
str.isupper()
Example
Let's check whether the first character is an uppercase letter using str[0].isupper() −
text1 = "Welcome to Tutorialspoint"
text2 = "hello world"
if text1[0].isupper():
print("Text1: Starts with a capital letter")
else:
print("Text1: Does not start with a capital letter")
if text2[0].isupper():
print("Text2: Starts with a capital letter")
else:
print("Text2: Does not start with a capital letter")
Text1: Starts with a capital letter Text2: Does not start with a capital letter
Using str.startswith() Method
The startswith() method checks whether a string starts with a specified prefix. We can pass a tuple of all uppercase letters to check if the string begins with any uppercase character.
Syntax
str.startswith(value, start, end)
Example
Here we use string.ascii_uppercase to create a tuple of all uppercase letters −
import string
text1 = "TutorialsPoint"
text2 = "python programming"
if text1.startswith(tuple(string.ascii_uppercase)):
print("Text1: Starts with a capital letter")
else:
print("Text1: Does not start with a capital letter")
if text2.startswith(tuple(string.ascii_uppercase)):
print("Text2: Starts with a capital letter")
else:
print("Text2: Does not start with a capital letter")
Text1: Starts with a capital letter Text2: Does not start with a capital letter
Using Regular Expressions
The re module supports regular expressions for pattern matching. We can use re.match() with the pattern [A-Z] to check if the first character is an uppercase letter.
Example
Using a regular expression to match uppercase letters at the beginning −
import re
text1 = "Welcome"
text2 = "welcome"
if re.match(r'[A-Z]', text1):
print("Text1: Starts with a capital letter")
else:
print("Text1: Does not start with a capital letter")
if re.match(r'[A-Z]', text2):
print("Text2: Starts with a capital letter")
else:
print("Text2: Does not start with a capital letter")
Text1: Starts with a capital letter Text2: Does not start with a capital letter
Comparison
| Method | Pros | Cons | Best For |
|---|---|---|---|
str[0].isupper() |
Simple and fast | Fails on empty strings | Simple validation |
startswith() |
Readable, handles edge cases | Requires import | Production code |
| Regular expressions | Flexible pattern matching | Slower, more complex | Complex patterns |
Conclusion
Use str[0].isupper() for simple cases, startswith() with string.ascii_uppercase for robust validation, and regular expressions when you need complex pattern matching. The startswith() method is generally recommended for production code.
