Check if String starts with a Lowercase in Python

This article will discuss how to check if a string starts with a lowercase letter in Python.

Table of Contents

Check if First Letter of String is Lowercase using islower()

The islower() function returns True if all the string characters are lower 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 islower() on it to check if the first character is lowercase or not. Let’s understand this with some examples,

Example 1:

sample_str = "the version"

# Check if string starts with an lowercase letter
if sample_str[0].islower():
    print("The String '%s' starts with a Lowercase Letter" % (sample_str))
else:
    print("First letter of String '%s' is not Lower case" % (sample_str))

Output:

The String 'the version' starts with a Lowercase Letter

In the string, the first letter was in lowercase.

Example 2:

sample_str = "Last version"

# Check if string starts with an lowercase letter
if sample_str[0].islower():
    print("The String '%s' starts with a Lowercase Letter" % (sample_str))
else:
    print("First letter of String '%s' is not Lower case" % (sample_str))

Output:

First letter of String 'Last version' is not Lower case

In the string, the first letter was not the lowercase letter.

Check if the First Letter of String is Lowercase 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 lowercase 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 lowercase letter.

Let’s see some examples,

Example 1:

import re

sample_str = "the version"

# Check if string starts with an lowercase letter
if re.search("^[a-z]", sample_str) is not None:
    print("The String '%s' starts with a Lowercase Letter" % (sample_str))
else:
    print("First letter of String '%s' is not Lower case" % (sample_str))

Output:

The String 'the version' starts with a Lowercase Letter

Here we used a string that started with an lowercase letter character.

Example 2:

import re

sample_str = "Last version"

# Check if string starts with an lowercase letter
if re.search("^[a-z]", sample_str) is not None:
    print("The String '%s' starts with a Lowercase Letter" % (sample_str))
else:
    print("First letter of String '%s' is not Lower case" % (sample_str))

Output:

First letter of String 'Last version' is not Lower case

It was a negative test. Here we used a string that starts with a upper case character in Python.

Summary

We learned two different ways to check if a string starts with an lowercase letter.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top