list to str in python

To convert a list to a string in Python, you can use the join() method. Here's an example:

main.py
my_list = ['Hello', 'world', '!']
string_result = ' '.join(my_list)
print(string_result)
89 chars
4 lines

This will output:

main.py
Hello world !
14 chars
2 lines

In this example, the join() method concatenates all the elements of the list my_list into a single string, separating them with a space.

You can also specify a different separator by passing it as an argument to the join() method, as shown below:

main.py
my_list = ['Hello', 'world', '!']
string_result = '-'.join(my_list)
print(string_result)
89 chars
4 lines

Output:

main.py
Hello-world-!
14 chars
2 lines

Here, the elements of my_list are joined with a hyphen as the separator.

related categories

gistlibby LogSnag