Python | Test if number is valid Excel column
Last Updated :
02 May, 2023
Sometimes, while working with Python strings, we can have a problem in which we need to test for string if it's a valid Excel column. This has application in many domains including day-day programming, web development, and Data Science. Let us discuss certain ways in which this task can be performed.
Method 1: Using re.match() + group()
The combination of the above functions can be used to perform this task. In this, we perform regex match() to match with the potential updated excel version which have A-XDF, and starts with 0-9 followed by 0-6 characters not exceeding 1048576. The groupby() is used to group the elements on that basis.
Python3
# Python3 code to demonstrate working of
# Test if number is valid Excel column
# Using re.match() + groupby()
import re
# initializing string
test_str = "C101"
# printing original string
print("The original string is : " + test_str)
# Test if number is valid Excel column
# Using re.match() + groupby()
temp = re.match(
r'^([A-Z]{1, 2}|[A-W][A-Z]{2}|X[A-E][A-Z]|XF[A-D])([1-9]\d{0, 6})$', test_str)
res = bool(temp) and int(temp.group(2)) < 1048577
# printing result
print("Is string valid excel column : " + str(res))
Output : The original string is : C101
Is string valid excel column : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2: Using Column Letter to Number Conversion
Explanation:
- We first initialize the input string test_str.
- We use a for loop to iterate through each character in the input string except the last character (which represents the row number). Inside the loop, we convert each character to a corresponding number between 1 and 26 using the ord() function (which returns the ASCII code of a character) and subtract 64 (the ASCII code of 'A' minus 1). We then multiply this number by 26 raised to the appropriate power based on the position of the character in the input string (e.g., the leftmost character is multiplied by 26 raised to the power of len(test_str)-2, the second-leftmost character is multiplied by 26 raised to the power of len(test_str)-3, etc.). We add up these products to obtain the column number.
- We check if the column number is between 1 and 16384 (the maximum number of columns in Excel). If it is, we set is_valid to True.
- Finally, we print the value of is_valid.
Python3
# Initializing string as input
test_str = "C101"
# Printing original string
print("The original string is : " + test_str)
# Test if number is valid Excel column
is_valid = False
# Convert column letters to column number
col_num = 0
for i in range(len(test_str)-1):
col_num += (ord(test_str[i]) - 64) * 26**(len(test_str)-i-2)
# Check if column number is within valid range
if col_num >= 1 and col_num <= 16384:
is_valid = True
# Printing resultant string
print("Is string valid excel column : " + str(is_valid))
OutputThe original string is : C101
Is string valid excel column : True
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(1), since we only use a few variables to store the intermediate results.
Similar Reads
Python Check If String is Number In Python, there are different situations where we need to determine whether a given string is valid or not. Given a string, the task is to develop a Python program to check whether the string represents a valid number.Example: Using isdigit() MethodPython# Python code to check if string is numeric
6 min read
Python - Check if String contains any Number We are given a string and our task is to check whether it contains any numeric digits (0-9). For example, consider the following string: s = "Hello123" since it contains digits (1, 2, 3), the output should be True while on the other hand, for s = "HelloWorld" since it has no digits the output should
2 min read
Copy Rows and Columns in Excel Using Python Manipulating data in Excel often involves tasks such as copying specific rows or columns based on certain conditions. Python, with its powerful libraries like Pandas and openpyxl, provides efficient solutions for handling Excel files. In this article, we will explore how to copy rows and columns in
3 min read
Check if a Number is a Whole Number in Python Checking if a Number is a Whole Number in Python means determining whether the number has no fractional part, using custom logic instead of relying on assumptions about its type. For example, a number like 7.0 is considered whole, while 7.5 is not. Letâs explore various methods to do it accurately.U
2 min read
Python | Check whether a string is valid json or not Given a Python String, the task is to check whether the String is a valid JSON object or not. Let's try to understand the problem using different examples in Python. Validate JSON String in PythonThere are various methods to Validate JSON schema in Python here we explain some generally used methods
3 min read
Input Validation in Python String In Python, string input validation helps ensure that the data provided by the user or an external source is clean, secure and matches the required format. In this article, we'll explore how to perform input validation in Python and best practices for ensuring that strings are correctly validated.Pyt
2 min read