This:
A = b'abcdefghijklmnopqrstuvwxyz0123456789!'
n = len(A)
def f(s):
t = 0
for i, c in enumerate(s):
t += A.index(c) * n ** (3 - i)
return t
print(f(b'aaaa')) # first possible 4-char string, should be 0
print(f(b'aaab')) # 2nd possible 4-char string, should be 1
print(f(b'!!!!')) # last possible 4-char string, should be 37^4 - 1
works to find the index of a 4-char string among all possible 4-char strings made with an alphabet A, but it does not seem efficient because of the many A.index() calls.
How to speed-efficiently find the index of a 4-char string among all possible 4-char strings made with an alphabet A?