Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 ?
char1 = "A"
char2 = "b"
print("Checking if the character", char1, "is uppercase or not")
print(char1.isupper())
print("Checking if the character", char2, "is uppercase or not")
print(char2.isupper())
Following is the output obtained ?
Checking if the character A is uppercase or not True Checking if the character b is uppercase 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 letter and returns True if the character is uppercase, otherwise False.
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
char1 = "A"
char2 = "b"
print("Checking if the character", char1, "is uppercase or not")
print(bool(re.match('[A-Z]', char1)))
print("Checking if the character", char2, "is uppercase or not")
print(bool(re.match('[A-Z]', char2)))
We get the output as shown below ?
Checking if the character A is uppercase or not True Checking if the character b is uppercase 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
char1 = 'A'
char2 = 'b'
print("Checking whether", char1, "is uppercase")
print(check_upper(char1))
print("Checking whether", char2, "is uppercase")
print(check_upper(char2))
The output obtained is as shown below ?
Checking whether A is uppercase True Checking whether b is uppercase 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'
char1 = 'A'
char2 = 'b'
print("Checking whether", char1, "is uppercase")
print(check_upper(char1))
print("Checking whether", char2, "is uppercase")
print(check_upper(char2))
The result produced is as follows ?
Checking whether A is uppercase True Checking whether b is uppercase False
Comparison
| Method | Pros | Cons | Best For |
|---|---|---|---|
isupper() |
Simple, handles Unicode | None | General use |
| Regular expressions | Flexible patterns | Slower, only English | Complex patterns |
| ASCII values | Fast, educational | Only English letters | Learning ASCII |
| Character comparison | Fast, readable | Only English letters | Simple range checks |
Conclusion
The isupper() method is the most recommended approach as it's simple and handles Unicode characters. Use ASCII values or character comparison for learning purposes or when dealing specifically with English letters.
