jeudi 26 mars 2015

Inserting strings into lists in Python (v. 3.4.2)

I am learning about Functions and Classes in Python 3.4.2, and I got a little sidetracked by the output from this code snippet:



print("This program will collect your demographic information and output it")
print ("")

class Demographics: #This class contains functions to collect demographic info

def phoneFunc(): #This function will collect user's PN, including area code
phoneNum = str(input("Enter your phone number, area code first "))
phoneNumList = []
phoneNumList[:0] = phoneNum
#phoneNumList.insert(0, phoneNum) this is commented out b/c I tried this and it made the next two lines insert the dash incorrectly

phoneNumList.insert(3, '-')
phoneNumList.insert(7, '-')
print(*phoneNumList)

x = Demographics
x.phoneFunc()


When it prints the phone number, it spaces the digits out like this: x x x - x x x - x x x x rather than xxx-xxx-xxxx.


Is there a way to remove the spaces between the characters? I've looked at these threads (the first one was the most helpful, and got me partly on my way) but I suspect my problem isn't exactly the same as what's described in them:


Inserting a string into a list without getting split into characters


Split string into a list in Python


python 3.4.2 joining strings into lists


Aucun commentaire:

Enregistrer un commentaire