how can I export a list to txt without commas and quotes

fruits = ['bananas', 'apples', 'oranges', 'strawberry']
with open("fruits_text.txt", 'w') as totxt_file:
    totxt_file.write(str(fruits)+'n')

The “n” in code above doesnt work, when I run it here is what I got

['bananas', 'apples', 'oranges', 'strawberry']

How can I export it without commas and quotes? like this

bananas
apples 
oranges
strawberry 

Answer

One way using str.join:

fruits = ['bananas', 'apples', 'oranges', 'strawberry']
with open("fruits_text.txt", 'w') as totxt_file:
    totxt_file.write("n".join(fruits))

Note that this doesn’t insert a line separator (n here) at the very end of the last line, which might be problematic in some cases.

Output:

# cat fruits_text.txt
bananas
apples
oranges
strawberry