Write a Python program that will ask the user to enter a string of lower-case characters and then print its corresponding two-digit code. For example, if the input is "home", the output should be "08151305".
Currently I have my code working to make a list of all the number, but I cannot
get it to add a 0 in front of the single digit numbers.
def word ():
output = []
input = raw_input("please enter a string of lowercase characters: ")
for character in input:
number = ord(character) - 96
output.append(number)
print output
This is the output I get:
word()
please enter a string of lowercase characters: abcdefghijklmnopqrstuvwxyz
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
I think I may need to change the list to a string or to integers to do this but
I am not sure how to do that.
解决方案
output.append("%02d" % number) should do it. This uses Python string formatting operations to do left zero padding.