'convert string of numbers to letters python

I need to convert a string of numbers into a string of letters to form words Example: if the input to your program is 1920012532082114071825463219200125320615151209190846 it should return stay hungry. stay foolish.

I have this so far:

def number ():
output = []
input = raw_input ("please enter a string of numbers: ")
for number in input:
    if number <= 26:
        character = chr(int(number+96))
        output.append(character)
    else:
        character = chr(int(number))
        output.append(character)
print output

I need it to somehow determine that every two numbers equals one letter. I have a program that does the reverse of this, outputs numbers when given letter. this is what that looks like:

def word ():
output = []
input = raw_input("please enter a string of lowercase characters: ")
for character in input:
    number = ord(character) - 96
    if number > 0:
        if number <= 9:
            output.append('0' + str(number))
        else:
            output.append(str(number))
    else:
        output.append(str(number + 96))
print ''.join(output)

Thanks for the help



Solution 1:[1]

Make a single iterator to your input string, and call zip with it like:

it = iter(data)
pairs = zip(it, it)

Gives:

[('1', '9'), ('2', '0'), ('0', '1'), ('2', '5'), ('3', '2'), ('0', '8'), ('2', '1'), ('1', '4'), ('0', '7'), ('1', '8'), ('2', '5'), ('4', '6'), ('3', '2'), ('1', '9'), ('2', '0'), ('0', '1'), ('2', '5'), ('3', '2'), ('0', '6'), ('1', '5'), ('1', '5'), ('1', '2'), ('0', '9'), ('1', '9'), ('0', '8'), ('4', '6')]

Next pass this to map with a ''.join as the function to make integer strings:

>>> map(''.join, zip(it,it))
['19', '20', '01', '25', '32', '08', '21', '14', '07', '18', '25', '46', '32', '19', '20', '01', '25', '32', '06', '15', '15', '12', '09', '19', '08', '46']

Now pass this to map again, to convert to ints:

>>> map(int, map(''.join, zip(it,it)))
[19, 20, 1, 25, 32, 8, 21, 14, 7, 18, 25, 46, 32, 19, 20, 1, 25, 32, 6, 15, 15, 12, 9, 19, 8, 46]

Now pass this to map with a lambda to perform your decoding logic:

>>> map(lambda n : chr(n+96) if n < 27 else chr(n), map(int, map(''.join, zip(it,it))))
['s', 't', 'a', 'y', ' ', 'h', 'u', 'n', 'g', 'r', 'y', '.', ' ', 's', 't', 'a', 'y', ' ', 'f', 'o', 'o', 'l', 'i', 's', 'h', '.']

And lastly, pass this to ''.join:

>>> ''.join(map(lambda n : chr(n+96) if n < 27 else chr(n), map(int, map(''.join, zip(it,it)))))
'stay hungry. stay foolish.'

What could be simpler? :)

Solution 2:[2]

''.join(map(lambda n: [chr(n),chr(n+96)][n<=26], map(int,map(''.join,zip(*[iter(s)]*2)))))

Solution 3:[3]

def remove(string): 

    return string.replace(" ", "")

dict = 

{'a': '1',
    'b': '2',
    'c': '3',
    'd': '4',
    'e': '5',
    'f': '6',
    'g': '7',
    'h': '8',
    'i': '9',
    'j': '10',
    'k': '11',
    'l': '12',
    'm': '13',
    'n': '14',
    'o': '15',
    'p': '16',
    'q': '17',
    'r': '18',
    's': '19',
    't': '20',
    'u': '21',
    'v': '22',
    'w': '23',
    'x': '24',
    'y': '25',
    'z': '26',
    '1': 'a',
    '2': 'b',
    '3': 'c',
    '4': 'd',
    '5': 'e',
    '6': 'f',
    '7': 'g',
    '8': 'h',
    '9': 'i',
    '10': 'j',
    '11': 'k',
    '12': 'l',
    '13': 'm',
    '14': 'n',
    '15': 'o',
    '16': 'p',
    '17': 'q',
    '18': 'r',
    '19': 's',
    '20': 't',
    '21': 'u',
    '22': 'v',
    '23': 'w',
    '24': 'x',
    '25': 'y',
    '26': 'z',
}


word =

 remove(input(''))

for x in word:

    print(dict[x])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 PaulMcG
Solution 2
Solution 3 dbc