1. 程式人生 > >python實現列表中各元素的拼接

python實現列表中各元素的拼接

and have 輸出 pytho strong pre 列表 class code

功能要求:

    假定有下面這樣的列表:
     names = [‘pear‘, ‘peach‘, ‘banana‘, ‘apple‘]
   輸出結果為:‘I have pear, peach, banana and apple.‘

源代碼如下:

names = [fentiao, fendai, fensi, apple]
print I have  + ,.join(names[:-1]) +  and  + names[-1]

結果如下:

python實現列表中各元素的拼接