How do you decode numbers to letters in python?

I was wondering if it is possible to convert numbers into their corresponding alphabetical value. So

1 -> a
2 -> b

I was planning to make a program which lists all the alphabetical combinations possible for a length specified by a user.

See I know how to build the rest of the program except this! Any help would be wonderful.

How do you decode numbers to letters in python?

vaultah

41.6k12 gold badges110 silver badges138 bronze badges

asked Apr 21, 2014 at 14:47

2

Big Letter:

chr(ord('@')+number)

1 -> A

2 -> B

...

Small Letter:

chr(ord('`')+number)

1 -> a

2 -> b

...

answered Sep 25, 2017 at 7:43

How do you decode numbers to letters in python?

LinconFiveLinconFive

1,4421 gold badge17 silver badges24 bronze badges

1

import string
for x, y in zip(range(1, 27), string.ascii_lowercase):
    print(x, y)

or

import string
for x, y in enumerate(string.ascii_lowercase, 1):
    print(x, y)

or

for x, y in ((x + 1, chr(ord('a') + x)) for x in range(26)):
    print(x, y)

All of the solutions above output lowercase letters from English alphabet along with their position:

1 a
...
26 z

You'd create a dictionary to access letters (values) by their position (keys) easily. For example:

import string
d = dict(enumerate(string.ascii_lowercase, 1))
print(d[3]) # c

answered Apr 21, 2014 at 14:49

How do you decode numbers to letters in python?

vaultahvaultah

41.6k12 gold badges110 silver badges138 bronze badges

You can use chr() to turn numbers into characters, but you need to use a higher starting point as there are several other characters in the ASCII table first.

Use ord('a') - 1 as a starting point:

start = ord('a') - 1
a = chr(start + 1)

Demo:

>>> start = ord('a') - 1
>>> a = chr(start + 1)
>>> a
'a'

Another alternative is to use the string.ascii_lowercase constant as a sequence, but you need to start indexing from zero:

import string

a = string.ascii_lowercase[0]

answered Apr 21, 2014 at 14:49

Martijn PietersMartijn Pieters

984k273 gold badges3871 silver badges3232 bronze badges

0

What about a dictionary?

>>> import string
>>> num2alpha = dict(zip(range(1, 27), string.ascii_lowercase))
>>> num2alpha[2]
b
>>> num2alpha[25]
y

But don't go over 26:

>>> num2alpha[27]
KeyError: 27

But if you are looking for all alphabetical combinations of a given length:

>>> import string
>>> from itertools import combinations_with_replacement as cwr
>>> alphabet = string.ascii_lowercase
>>> length = 2
>>> ["".join(comb) for comb in cwr(alphabet, length)]
['aa', 'ab', ..., 'zz']

answered Apr 21, 2014 at 14:56

wflynnywflynny

17.5k5 gold badges43 silver badges63 bronze badges

0

Try a dict and some recursion:

def Getletterfromindex(self, num):
    #produces a string from numbers so

    #1->a
    #2->b
    #26->z
    #27->aa
    #28->ab
    #52->az
    #53->ba
    #54->bb

    num2alphadict = dict(zip(range(1, 27), string.ascii_lowercase))
    outval = ""
    numloops = (num-1) //26

    if numloops > 0:
        outval = outval + self.Getletterfromindex(numloops)

    remainder = num % 26
    if remainder > 0:
        outval = outval + num2alphadict[remainder]
    else:
        outval = outval + "z"
    return outval

answered Oct 2, 2019 at 15:42

Here is a quick solution:

# assumes Python 2.7
OFFSET = ord("a") - 1

def letter(num):
    return chr(num + OFFSET)

def letters_sum_to(total):
    for i in xrange(1, min(total, 27)):
        for rem in letters_sum_to(total - i):
            yield [letter(i)] + rem
    if total <= 26:
        yield [letter(total)]

def main():
    for letters in letters_sum_to(8):
        print("".join(letters))

if __name__=="__main__":
    main()

which produces

aaaaaaaa
aaaaaab
aaaaaba
aaaaac
aaaabaa
aaaabb
aaaaca
aaaad
aaabaaa
# etc

Note that the number of solutions totalling to N is 2**(N-1).

answered Apr 21, 2014 at 16:06

Hugh BothwellHugh Bothwell

53.6k7 gold badges81 silver badges98 bronze badges

for i in range(0, 100):
     mul = 1
     n   = i
     if n >= 26:
         n   = n-26
         mul = 2
     print chr(65+n)*mul

answered Feb 7, 2017 at 9:42

Not the answer you're looking for? Browse other questions tagged python python-3.x numerical alphabetical or ask your own question.

How do you decode numbers in the alphabet?

The Letter-to-Number Cipher (or Number-to-Letter Cipher or numbered alphabet) consists in replacing each letter by its position in the alphabet , for example A=1, B=2, Z=26, hence its over name A1Z26 .

How do you convert a number to a string in Python?

In Python an integer can be converted into a string using the built-in str() function. The str() function takes in any python data type and converts it into a string.

How do you convert to Z in Python?

We can convert letters to numbers in Python using the ord() method. The ord() method takes a single character as an input and return an integer representing the Unicode character. The string can be iterated through for loop and use an ord() method to convert each letter into number.

How do you encode an alphabet in Python?

Use the ord() and chr() functions to turn characters to numbers, and vice versa. The numbers for letters and digets correspond to the ASCII standard. To turn that to numbers between 1 and 26, subtract 64, or use ord('@') (also 64).