
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a String Contains Only Lower Case Letters in Python
Checking for Lowercase Letters in Python
In Python, a string is considered to have only lower-case letters if all its characters are alphabets and each one of them is in lower case (from 'a' to 'z').
We can verify this using the built-in islower() method, regular expressions, or by checking each character using the all() method.
Using islower() Method
The islower() method returns True if all the characters in the string are in lowercase and there is at least one alphabet. If the string contains uppercase letters or non-alphabetic characters, it returns False.
Example: All Lowercase Letters
In the following example, the string contains only lowercase alphabetic characters, so the islower() method returns True -
str1 = "pythonrules" print("The given string is") print(str1) print("Checking if the string contains only lowercase letters") print(str1.islower())
We get the output as follows -
The given string is pythonrules Checking if the string contains only lowercase letters True
Example: Mixed Characters
In the following example, the string includes digits and uppercase letters, so the islower() method returns False -
str2 = "python3Rocks" print("The given string is") print(str2) print("Checking if the string contains only lowercase letters") print(str2.islower())
The output is -
The given string is python3Rocks Checking if the string contains only lowercase letters False
Using Regular Expressions
You can use the re.fullmatch() function to check if a string contains only upper-case letters. This function accepts a pattern and a string as parameters and verifies whether the given string is in the specified pattern.
The pattern [a-z]+ matches only strings that contain one or more lowercase letters.
Example: Using re.fullmatch() Function
In the following example, we use regular expressions to check if the string contains only lowercase letters from 'a' to 'z' -
import re str3 = "lowercaseonly" print("The given string is") print(str3) print("Checking if the string contains only lowercase letters") print(bool(re.fullmatch(r"[a-z]+", str3)))
The result is -
The given string is lowercaseonly Checking if the string contains only lowercase letters True
Example: String with uppercase Letters
In the following example, the string has a space and a digit. So the regular expression check returns False -
import re str4 = "lowercase 123" print("The given string is") print(str4) print("Checking if the string contains only lowercase letters") print(bool(re.fullmatch(r"[a-z]+", str4)))
The output is -
The given string is lowercase 123 Checking if the string contains only lowercase letters False
Using Character-wise Check
The islower() method checks for lower, and the isalpha() method checks for alphabets. The all() function returns true if all of its elements are true.
You can also loop through each character in the string and verify if it is a lowercase alphabet using the islower() and isalpha() methods together. If all characters result in true for both methods, the string is valid.
Example: Using all() Method
In the following example, we manually check each character and confirm it is a lowercase letter -
def is_all_lowercase(string): return all(char.islower() and char.isalpha() for char in string) str5 = "checklowercase" print("The given string is") print(str5) print("Checking if the string contains only lowercase letters") print(is_all_lowercase(str5))
The output is shown below -
The given string is checklowercase Checking if the string contains only lowercase letters True