
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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
- Related Articles
- How to test if a letter in a string is uppercase or lowercase using javascript?
- Python Program to check if a string starts with a substring using regex
- Java Program to check if any String in the list starts with a letter
- How can I filter string value with starting letter?
- How can we add an underscore before each capital letter inside a java String?
- How to check if string or a substring of string starts with substring in Python?
- How i can replace number with string using Python?
- How can I disable JavaScript click function if link starts with #?
- How to check if a string starts with a specified Prefix string in Golang?
- How can I fill out a Python string with spaces?
- How can I tell if a string repeats itself in Python?
- How to check whether a string starts with XYZ in Python?
- How to check if a character in a string is a letter in Python?
- Check if a string starts with given word in PHP
- Return TRUE if the first string starts with a specific second string JavaScript
