
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 Upper Case Letters in Python
A string is a collection of characters that can represent a single word or an entire statement. Strings are simple to use in Python since they do not require explicit declaration and may be defined with or without a specifier. Python has various built in functions and methods for manipulating and accessing strings. Because everything in Python is an object, a string is an object of the String class with several methods.
In this article, we are going to find out how to check if a string contains only upper case letters in python.
Using the isupper() function
One way to verify for the uppercase letters is using the string library's isupper() function. If every character in current string is uppercase, this function returns True; otherwise, it returns False.
Example 1
In the example given below, we are taking 2 strings str1 and str2, and checking if they contain any characters other than lower case alphabets. We are checking with the help of the isupper() function.
str1 = 'ABCDEF' str2 = 'Abcdef' print("Checking whether",str1,"is upper case") print(str1.isupper()) print("Checking whether",str2,"is upper case") print(str2.isupper())
Output
The output of the above program is,
('Checking whether', 'ABCDEF', 'is upper case') True ('Checking whether', 'Abcdef', 'is upper case') False
Example 2
Following is another example of this using the isupper() function. In the program given below, we are checking what would happen if there are spaces in between the uppercase words.
str1 = 'WELCOME TO TUTORIALSPOINT' print("Checking whether",str1,"is upper case") print(str1.isupper())
Output
The output of above program is,
('Checking whether', 'WELCOME TO TUTORIALSPOINT', 'is upper case') True
Using the regular expressions
We can also use the regular expressions to determine whether the given string contains lower case letters.
To do so, import the re library and install it if it isn't already installed. We'll use the regular expression "[A Z]+$" after importing the re library. If the string contains any characters other than uppercase characters, this will return False; otherwise, True will be returned.
Example
In this program given below, we are using the regular expression ?[A Z]+$' to check whether the given string is uppercased or not.
import re str1 = 'ABCDEF' str2 = 'Abcdef' print("Checking whether",str1,"is upper case") print(bool(re.match('[A Z]+$', str1))) print("Checking whether",str2,"is uppercase") print(bool(re.match('[A Z]+$', str2)))
Output
The output to the above program is,
('Checking whether', 'ABCDEF', 'is upper case') False ('Checking whether', 'Abcdef', 'is uppercase') False
Using ASCII values
We can iterate through each character of the string and verify based on the ASCII values. We know that the ASCII values of the upper case characters are between 65 and 90. True is returned if each ASCII value is greater than 64 and less than 91; otherwise, false is returned.
Example
In the below given example, we are writing a function checkupper() and comparing the ASCII values for every character in that string.
def checkupper(str1): n = len(str1) count = 0 for i in str1: if(64<ord(i) <91): count += 1 if count == n: return True return False str1 = 'ABCDEF' str2 = 'Abcdef' print("Checking whether",str1,"is upper case") print(checkupper(str1)) print("Checking whether",str2,"is upper case") print(checkupper(str2))
Output
The output of the above program is,
('Checking whether', 'ABCDEF', 'is upper case') True ('Checking whether', 'Abcdef', 'is upper case') None