How do you turn a list into a string in Python?

You can use the join() method to concatenate the elements in a list into a string.

The sample code is shown below:

my_list = ['apple', 'banana', 'orange']
my_string = ' '.join(my_list)
print(my_string)

The output is:

apple banana orange

In the join() method, you can specify a separator for the concatenated string, such as the space in the example code. If no separator is specified, the elements in the list will be concatenated with an empty string by default.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds