This article will discuss how to use the isupper() method of String class in Python.
Table of Contents
In Python, the string class (Str) provides a member function isupper() to check if a string is an uppercase string or not.
Syntax of isupper() method
Str.isupper()
Parameters:
- It does not accept any parameter.
Returns:
- It returns True if the calling string object contains all the uppercase characters. Whereas, it returns False if any of the characters in the string is of lowercase.
- It also returns False, if the string does not contain an upper case character.
Examples of issupper() method of string in Python
Check if a string is an uppercase string or not
We can check if a string is an uppercase string or not using isuuper() function. If all the the string characters are upper case characters, then isupper() returns True.
Frequently Asked:
- Check if string ends with any string from List in Python
- Count Occurrences of a character in String in Python
- Remove all whitespace from a string in Python
- How to Reverse a String in Python?
Example 1:
str_obj = 'SAMPLE STRING' if str_obj.isupper(): print('String is an uppercase string') else: print('String is not an uppercase string')
Output
String is an uppercase string
Example 2:
str_obj = 'Sample String' if str_obj.isupper(): print('String is an uppercase string') else: print('String is not an uppercase string')
Output:
String is not an uppercase string
As few characters in the string are lower case characters, therefore, isupper() returned False.
Check if a string containing numbers is uppercase or not
Suppose we have a string containing numbers only. Let’s check if this string is an uppercase string or not using isupper(),
str_obj = '123 345' if str_obj.isupper(): print('String is an uppercase string') else: print('String is not an uppercase string')
Output:
String is not an uppercase string
As string does not have any upper case character, so isupper() returned False.
Check if a string containing letters & numbers is uppercase or not
Suppose we have a string containing numbers and some upper case letters. Let’s check if this string is an uppercase string or not using isupper(),
str_obj = 'Simply 123' if str_obj.isupper(): print('String is an uppercase string') else: print('String is not an uppercase string')
Output:
String is not an uppercase string
As string does not have any lower case character, but has one or more upper case characters. So isupper() returned True.
Check if a character is uppercase or not
There is no data type for individual characters in Python. A single character is also a python string object. So, we can use isupper() method to check if a character is upper case character or not,
str_obj = 'S' if str_obj.isupper(): print('Character is an uppercase character') else: print('Character is not an uppercase character')
Output:
Character is an uppercase character
Summary
Using isupper() function, we can check if a string is uppercase string or not.