# define a list my_list = ['D', 'e', 'v', 's', 'h', 'e', 'e', 't'] # use join() function to convert list to string result = "".join(my_list) print(result)
In Python, the built-in function join() can be used to convert a list of strings into a single string. This function is useful when you need to manipulate or display a list of strings as a single entity.
For example, you might want to convert a list of customer names into a single string that can be displayed in a customer list report. Or you might want to convert a list of filenames into a single string that can be passed to a file-processing program.
name_parts = ['Justin', 'Tray', 'Lambart'] print("List is: ", name_parts) name_str = " ".join(name_parts) print("Final string is: ", name_str)
List is: ['Justin', 'Tray', 'Lambart'] Final string is: Justin Tray Lambart
This code creates a list called name_parts containing the strings 'Justin', 'Tray', and 'Lambart'. It then joins these strings together into a single string called name_str using the space character as a separator.
If you do not want to use any method to convert a List to String, you can also use Python For loop. We will be applying for loop on the list and in each iteration, we will append the item to String variable result.
my_list = ['D', 'e', 'v', 's', 'h', 'e', 'e', 't'] result = '' for item in my_list: result += item print(result)
words = ["Hello", "World"] result = " ".join(words) print(result) # -> Hello World