function int_to_alpha
Converts a non-negative integer to an Excel-style alphabetic column label (A, B, C, ..., Z, AA, AB, ..., ZZ, AAA, etc.).
/tf/active/vicechatdev/patches/util.py
1085 - 1103
moderate
Purpose
This function generates alphabetic labels commonly used in spreadsheet applications like Excel for column headers. It converts zero-based integer indices to their corresponding alphabetic representation, supporting both uppercase (A-Z) and lowercase (a-z) output. The function handles multi-character labels by extending beyond Z to AA, AB, etc., similar to how spreadsheet columns are labeled.
Source Code
def int_to_alpha(n, upper=True):
"Generates alphanumeric labels of form A-Z, AA-ZZ etc."
casenum = 65 if upper else 97
label = ''
count= 0
if n == 0: return str(chr(n + casenum))
while n >= 0:
mod, div = n % 26, n
for _ in range(count):
div //= 26
div %= 26
if count == 0:
val = mod
else:
val = div
label += str(chr(val + casenum))
count += 1
n -= 26**count
return label[::-1]
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
n |
- | - | positional_or_keyword |
upper |
- | True | positional_or_keyword |
Parameter Details
n: A non-negative integer representing the index to convert. 0 corresponds to 'A' (or 'a'), 1 to 'B' (or 'b'), 25 to 'Z' (or 'z'), 26 to 'AA' (or 'aa'), etc. Must be >= 0.
upper: Boolean flag to determine case of output. If True (default), returns uppercase letters (A-Z). If False, returns lowercase letters (a-z).
Return Value
Returns a string containing the alphabetic label corresponding to the input integer. For n=0, returns 'A' (or 'a'). For n=25, returns 'Z' (or 'z'). For n=26, returns 'AA' (or 'aa'). The length of the string increases as n grows larger, following the pattern: single letters (0-25), double letters (26-701), triple letters (702+), etc.
Usage Example
# Basic usage
result1 = int_to_alpha(0) # Returns 'A'
result2 = int_to_alpha(25) # Returns 'Z'
result3 = int_to_alpha(26) # Returns 'AA'
result4 = int_to_alpha(51) # Returns 'AZ'
result5 = int_to_alpha(701) # Returns 'ZZ'
result6 = int_to_alpha(702) # Returns 'AAA'
# Lowercase output
result7 = int_to_alpha(0, upper=False) # Returns 'a'
result8 = int_to_alpha(26, upper=False) # Returns 'aa'
# Use case: Generate column labels for a table
column_labels = [int_to_alpha(i) for i in range(30)]
print(column_labels) # ['A', 'B', 'C', ..., 'Z', 'AA', 'AB', 'AC', 'AD']
Best Practices
- Only pass non-negative integers to this function; negative values will cause unexpected behavior
- This function is useful for generating column headers in data tables, spreadsheets, or any UI requiring alphabetic sequential labels
- The function uses a base-26-like system but with a special handling where 'A' starts at 0, not 1
- For large values of n, the function will generate increasingly long strings (e.g., n=18278 produces 'AAAA')
- The algorithm reverses the label at the end, so intermediate string building is done in reverse order
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function int_to_roman 56.9% similar
-
function capitalize_unicode_name 49.6% similar
-
function _int_to_bytes 42.4% similar
-
function capitalize 39.0% similar
-
function sanitize_folders 36.9% similar