I tried really hard on this one, finally came up with this, however, there's space between every number in my result because of the code 'print xxx,' , I want to find a way to delete those spaces.
Program: let user enter say 555-GET-FOOD, convert it to 555-438-3663

alph = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p',\
            'q','r','s','t','u','v','w','x','y','z']
num =[2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9]

phone = raw_input('enter phone number in the format xxx-xxx-xxxx ').lower()

index = 0
for index in range(len(phone)):
    if phone[index].isalpha():
        print num[alph.index(phone[index])],
    else:
        print phone[index],

this is a very intro python course, I did some reasearch online, people using dictionary and enu something or join that we never covered in class. besides the code I used in my program, I was trying to use 'replace', but failed.
It took me hours to came up with the code I already have but I want to make it perfect without the spaces, so any help will be appreciated.

There are many ways to do this. One of them is to append your characters to a list instead of printing them directly. Then you join the list items with an empty string and you print the result.

alph = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p',\
            'q','r','s','t','u','v','w','x','y','z']
num =[2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9]

phone = raw_input('enter phone number in the format xxx-xxx-xxxx ').lower()

accumulator = []
index = 0
for index in range(len(phone)):
    if phone[index].isalpha():
        accumulator.append(num[alph.index(phone[index])])
    else:
        accumulator.append(phone[index])
print ''.join(accumulator)

Thank you very much Gribouillis, but is there any way to do this without the join method? We haven't covered that in class yet.

The best way for your level would be to build up a string in the loop:

alph = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p',\
            'q','r','s','t','u','v','w','x','y','z']
num =[2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9]

# testing only 
phone = "555-GET-FOOD".lower()
#phone = raw_input('enter phone number in the format xxx-xxx-xxxx ').lower()

# build up from an empty string
s = ""
for index in range(len(phone)):
    if phone[index].isalpha():
        s = s + str(num[alph.index(phone[index])])
    else:
        s = s + phone[index]

print s  # 555-438-3663

Sneekula's way is probably the simplest if you don't want to use 'join'. If you were working with python 3.0 (which is obviously not the case), you could write

L = ['a', 'b', 'c', 'd', 'e']
for x in L:
    print(x, end= '')
print()

which would print abcde.
Before 3.0, the equivalent is

import sys
L = ['a', 'b', 'c', 'd', 'e']
for x in L:
    sys.stdout.write(x)
print

Thank you so much sneekula, this is exactly what I want this to be. I was wondering how I should build a string, because string is immutable. Now im clear. Thank you

Thanks Gribouillis. I am also trying the join method, just for me to learn. It's good

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.