
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to check if a character is upper-case in Python?
In this article, we are going to find out how to check if a character is an upper case in Python..
The first approach is by using the isupper() method. The Python standard library has a built-in method called isupper(). It supports the use of strings and other types of data. It shows whether a string of characters contains only capital letters.
If at least one character is lowercase, it returns FALSE. Otherwise, if every letter in the string is capitalised, it returns TRUE. It doesn't need any parameters.
Example
In the example given below, we are taking 2 strings as input and we are checking if they are uppercased or not using the isupper() method −
str1 = "A" str2 = "b" print("Checking if the string '",str1,"' is uppercased or not") print(str1.isupper()) print("Checking if the string '",str2,"' is uppercased or not") print(str2.isupper())
Output
The output of the above example is as shown below −
Checking if the string ' A ' is uppercased or not True Checking if the string ' b ' is uppercased or not False
Using Regular Expressions
Regular expressions are used in the second method. Import the re library and install it if it isn't already installed to use it. After importing the re library, we'll use the regular expression ‘[A-Z]’. This will return False if the character is lowercased, otherwise, it returns True.
Example
In the example given below, we are taking 2 characters as input and we are checking if they are upper cased or not using Regular expressions match method −
import re str1 = "A" str2 = "b" print("Checking if the string '",str1,"' is uppercased or not") print(bool(re.match('[A-Z]', str1))) print("Checking if the string '",str2,"' is uppercased or not") print(bool(re.match('[A-Z]', str2)))
Output
The output of the above example is given below −
Checking if the string ' A ' is uppercased or not True Checking if the string ' b ' is uppercased or not False
Using ASCII values
The third method involves the use of ASCII values. We know that the ASCII values for lower case characters start at 97, so we need to check if the character’s ASCII value is less than 97. If the ASCII value is less than 97, true is returned; otherwise, false is returned.
Example
In the example given below, we are taking 2 characters as input and we are checking if they are upper cased using ord() method by comparing ASCII values −
def checkupper(str): if ord(str) < 96 : return True return False str1 = 'A' str2 = 'b' 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 example is as shown below −
Checking whether A is upper case True Checking whether b is upper case False
Using Comparison
The fourth approach is by direct comparison of the given character. We will check if the character is greater than equal to “A” or less than equal to “Z”. If the character lies in this range, then True is returned else False is returned.
Example
In the example given below, we are taking 2 characters as input and checking if they are uppercased or not by comparing them with “A” and “Z” −
def checkupper(str): if str >= 'A' and str <= 'Z': return True else: return False str1 = 'A' str2 = 'b' 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 example is −
Checking whether A is upper case True Checking whether b is upper case False
- Related Articles
- How to check if a string contains only upper case letters in Python?
- Java Program to check whether the entered character a digit, white space, lower case or upper case character
- How to check if a character in a string is a letter in Python?
- C# Program to check if a character is a whitespace character
- How to check if a string contains only lower case letters in Python?
- Program to check if matrix is upper triangular in C++
- How to convert Lower case to Upper Case using C#?
- How to convert Upper case to Lower Case using C#?
- How to check if a given character is a number/letter in Java?
- Check if a character is alphanumeric in Arduino
- Check if a character is printable in Arduino
- How to fetch all databases with name having upper case character after some word using MySQL?
- Program to check if a string contains any special character in Python
- Converting to upper case in Java.
- How to generate random strings with upper case letters and digits in Python?
