在Python中将字符串插入列表(3.4.2版)

在Python中将字符串插入列表(3.4.2版),python,string,Python,String,我正在学习Python 3.4.2中的函数和类,下面代码片段的输出使我有点偏离了方向: 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 funct

我正在学习Python 3.4.2中的函数和类,下面代码片段的输出使我有点偏离了方向:

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()
当它打印电话号码时,会按如下方式将数字隔开: x x x x x x x x x x x x而不是xxx xxx xxxx

有没有办法删除字符之间的空格?我已经看过了这些线程(第一个线程是最有帮助的,并且让我部分上路了),但我怀疑我的问题与它们中描述的不完全相同:


目前,您正在向打印方法传递一个字符列表,如果未指定分隔符,每个字符都将以空格分隔(默认分隔符)打印

如果在print方法调用中将
sep
指定为空字符串,则字符之间将没有空格

>>> 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
另一种方法是使用
print(''.join(phoneNumList))

尝试这样做:

print(''.join(phoneNumList))

这会使用不带分隔符的字符将列表连接到字符串中。

为什么要首先创建列表?只需更改字符串:

>>> 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
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()
您还可以相当轻松地添加改进,例如检查分隔符是否已经存在(即,它是由用户输入的):


快速跟进:为什么添加“end=”\n"? 如果我没有弄错的话,\n命令会添加一行新行;这对我要做的事有必要吗?我这样问完全是出于好奇。非常感谢你的帮助。我很感激。@CSP.AT.FHS我把它放在那里,因为否则提示会从同一行开始。这是一种非常酷的修复代码的方法。我之所以列出一个列表,首先是因为我想看看如何将字符串更改为列表;我没有考虑过把它列在一张清单上,即使现在我知道你做了什么,我也可以把它列在一张清单上。谢谢你的见解。他们真的帮了大忙。这是一个有趣的解决方案+1.
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()