- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to 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
- Related Articles
- How to check if a string contains only lower case letters in Python?
- How to check if a string contains only whitespace letters in Python?
- Check if the String contains only unicode letters in Java
- How to check if a Python string contains only digits?
- Check if the String contains only unicode letters and space in Java
- Check if the String contains only unicode letters or digits in Java
- How to find If a given String contains only letters in Java?
- How to check if a string only contains certain characters in Python?
- How to check if a character is upper-case in Python?
- Check if the String contains only unicode letters, digits or space in Java
- How to check if a unicode string contains only numeric characters in Python?
- How to check if a string contains only decimal characters?
- How to check if a String contains another String in a case insensitive manner in Java?
- Method to check if a String contains a sub string ignoring case in Java
- How to generate random strings with upper case letters and digits in Python?
