
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
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 uppercase letter in Python. We will explore four different approaches -
- Using the isupper() method
- Using regular expressions
- Using ASCII values
- Direct comparison
In each approach, we will check whether a given character is in uppercase and return True or False accordingly.
Using the isupper() Method
The first and most straightforward way is by using the built-in isupper() method. This method returns True if the character is uppercase, and False otherwise.
Example
In the following example, we are checking whether two characters are uppercase 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())
Following is the output obtained -
Checking if the string ' A ' is uppercased or not True Checking if the string ' b ' is uppercased or not False
Using Regular Expressions
The second approach is to use regular expressions from Python's re module. The pattern [A-Z] matches any single uppercase English letters and will return False if the character is lowercase, otherwise, it returns True.
Example
In the following example, we use the re.match() function to check whether the character falls in the uppercase A-Z range -
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)))
We get the output as shown below -
Checking if the string ' A ' is uppercased or not True Checking if the string ' b ' is uppercased or not False
Using ASCII values
Each character has a corresponding ASCII value. Uppercase letters range from 65 ('A') to 90 ('Z'). So, we can check whether the ASCII value of the character lies within this range using the ord() function.
Example
In the following example, we define a function that checks if the ASCII value of the character is between 65 and 90 -
def check_upper(char): return 65 <= ord(char) <= 90 str1 = 'A' str2 = 'b' print("Checking whether", str1, "is upper case") print(check_upper(str1)) print("Checking whether", str2, "is upper case") print(check_upper(str2))
The output obtained is as shown below -
Checking whether A is upper case True Checking whether b is upper case False
Using Character Comparison
This method checks if the character falls between 'A' and 'Z' using direct comparison. If it does, the character is uppercase.
Example
In the following example, we compare the character with 'A' and 'Z' using string comparison -
def check_upper(char): return 'A' <= char <= 'Z' str1 = 'A' str2 = 'b' print("Checking whether", str1, "is upper case") print(check_upper(str1)) print("Checking whether", str2, "is upper case") print(check_upper(str2))
The result produced is as follows -
Checking whether A is upper case True Checking whether b is upper case False