Python 如何删除带有字符串的列表中的逗号,写一行代码

Python 如何删除带有字符串的列表中的逗号,写一行代码,python,string,python-2.7,list,Python,String,Python 2.7,List,我有一个列表,x=[“苹果”、“香蕉”、“胡萝卜”、“奶酪”、“梨”],我将写一行代码,执行时返回“苹果、香蕉、胡萝卜、奶酪和梨” 非常感谢你的帮助 你需要像octo说的那样使用join arr = ["apple", "banana", "carrot", "cheese", "pear"] my_string = " ".join(arr) 你也可以这样做: arr = ["apple", "banana", "carrot", "cheese", "pear"] my_string =

我有一个列表,x=[“苹果”、“香蕉”、“胡萝卜”、“奶酪”、“梨”],我将写一行代码,执行时返回“苹果、香蕉、胡萝卜、奶酪和梨”


非常感谢你的帮助

你需要像octo说的那样使用join

arr = ["apple", "banana", "carrot", "cheese", "pear"]
my_string = " ".join(arr) 
你也可以这样做:

arr = ["apple", "banana", "carrot", "cheese", "pear"]
my_string = ""
for ( n in range(len(arr) ):
  if n > 0:
    my_str += " " + arr[n]
  else:
    my_str += arr[n]

只是为了让我的答案看起来与其他人不同。

最好的答案来自Yousaf

x=[“苹果”、“香蕉”、“胡萝卜”、“奶酪”、“梨”]


打印(“\”“+'s”。加入(x[:-1])+“s”+”和“+(x[4])+“s”+“\”)

到目前为止您尝试了什么?检查
str.join()
函数:当我在空闲控制台中写入x.join()时,一个come:AttributeError:'list'对象没有属性'join'@Wander Nauta,谢谢你,我是来搜索的。我必须和谷歌一起搜索,我不必找到这个答案。。。。所以,谢谢你的链接。@iratxe
x=[“苹果”、“香蕉”、“胡萝卜”、“奶酪”、“梨”]print(“\”+”s)。加入(x[:-1])+“s”+”和“+(x[4])+”s“+”)
@yedpodtrzitko,我昨天有五个小时,今天有两个小时在谷歌上搜索,我正在写“python删除逗号字符串列表”所有的答案都是我要找的不是要找的。。。。