5

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

How to split a string into a list?

python 3.4.2 joining strings into lists

Community
  • 1
  • 1
CSP.AT.MASH
  • 103
  • 1
  • 1
  • 8

3 Answers3

3

As it currently stands, you are passing a list of characters to the print method, each of which will be printed space separated (default separator) if you do not specify a separator.

If we specify the sep as empty string in the print method call, there will be no spaces in between the characters.

>>> phoneNumList = []
>>> phoneNumList[:0] = "xxx-xxx-xxxx"
>>> phoneNumList
['x', 'x', 'x', '-', 'x', 'x', 'x', '-', 'x', 'x', 'x', 'x']
>>> print(*phoneNumList)
x x x - x x x - x x x x
>>> print(*phoneNumList, sep="", end="\n")
xxx-xxx-xxxx

The other approach is to join the characters and pass them as a single string input to the print method, using print(''.join(phoneNumList))

>>> print(''.join(phoneNumList))
xxx-xxx-xxxx
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • A quick follow up: why did you add the "end ="\n"? If I'm not mistaken, the \n command adds a new line; is that necessary for what I'm trying to do? I am asking solely out of curiosity. Thanks so much for your help. I appreciate it. – CSP.AT.MASH Mar 26 '15 at 16:52
  • @CSP.AT.FHS I put it i there because otherwise the prompt begins from the same line. – Anshul Goyal Mar 26 '15 at 17:01
2

Try doing this:

print(''.join(phoneNumList))

This joins the list into a string using no separator characters.

juhist
  • 4,210
  • 16
  • 33
2

Why bother making the list in the first place? Just change the string:

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 "))
        for position in (6, 3):
            phoneNum = phoneNum[:position] + '-' + phoneNum[position:]
        print(phoneNum)

x = Demographics
x.phoneFunc()

You can also add improvements fairly easily, like checking if the separator is already there (i.e, it was entered by the user):

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 "))
        phoneNum = phoneNum.replace('-', '') #Get rid of any dashes the user added
        for position in (6, 3):
            phoneNum = phoneNum[:position] + '-' + phoneNum[position:]
        print(phoneNum)

x = Demographics
x.phoneFunc()
Rick
  • 43,029
  • 15
  • 76
  • 119
  • This is a very cool way to fix my code. The reason I made a list in the first place is because I wanted to see how it would work to change a string to a list; I hadn't considered keeping it a list even though now that I see what you did I could have just left it a string. Thanks for the insights. They really helped a lot. – CSP.AT.MASH Mar 26 '15 at 16:54
  • this is an interesting solution. +1 – Anshul Goyal Apr 15 '15 at 19:54