The islower()
method is a built-in method in Python that returns True
if all alphabetic characters in a string are lowercase and there is at least one character in the string, otherwise it returns False
. This method is helpful when you want to check if a string contains only lowercase alphabetic characters or not.
Syntax
string.islower()
Here, string
is the string that we want to check for lowercase alphabetic characters.
Return Value
The islower()
method returns True
if all alphabetic characters in the string are lowercase and there is at least one character in the string. If the string is empty or contains uppercase alphabetic characters or non-alphabetic characters, then it returns False
.
Examples
Here are three different examples of how to use the islower()
method in Python:
Example 1: Checking if a String is Lowercase
string = "hello world" result = string.islower() print(result)
Output:
True
In this example, we define a string “hello world” and use the islower()
method to check if it contains only lowercase alphabetic characters. Since all the characters in the string are lowercase, the method returns True
.
Example 2: Checking if a String is not Lowercase
string = "Hello World" result = string.islower() print(result)
Output:
False
In this example, we define a string “Hello World” and use the islower()
method to check if it contains only lowercase alphabetic characters. Since the string contains uppercase characters, the method returns False
.
Example 3: Checking if a String is Empty
string = "" result = string.islower() print(result)
Output:
False
In this example, we define an empty string and use the islower()
method to check if it contains only lowercase alphabetic characters. Since the string is empty, the method returns False
.
Use Cases
The islower()
method can be useful in a variety of scenarios, such as:
- Validating user input to ensure that it only contains lowercase alphabetic characters
- Checking if a given string is in lowercase before performing certain operations
- Identifying the presence of uppercase characters in a string