I have a List , x = ["apple", "banana", "carrot", "cheese", "pear"] and I will write in one line of code that when executed returns "apples bananas carrots cheeses and pears".
Very Thanks for your help!
I have a List , x = ["apple", "banana", "carrot", "cheese", "pear"] and I will write in one line of code that when executed returns "apples bananas carrots cheeses and pears".
Very Thanks for your help!
You need to use join just like octo has said
arr = ["apple", "banana", "carrot", "cheese", "pear"]
my_string = " ".join(arr)
Well you can also do:
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]
just to make my answer look different than others.
The Best answer is from Yousaf .
x = ["apple", "banana", "carrot", "cheese", "pear"]
print("\""+'s '.join(x[:-1])+"s"+" and "+(x[4])+"s"+"\"")