How to convert a string to a list in Python?
To convert a string to a list, you can use the split() method.
For example, suppose we have a string “apple banana orange” and we want to convert it to a list containing each fruit, we can use the following code:
string = "apple banana orange"
fruit_list = string.split()
print(fruit_list)
The output is: [‘apple’, ‘banana’, ‘orange’]
By default, the split() method uses a space as a delimiter to split a string into multiple substrings and returns a list. If you want to use a different delimiter, you can specify it in the split() method, for example:
string = "apple,banana,orange"
fruit_list = string.split(',')
print(fruit_list)
The output is: [‘apple’, ‘banana’, ‘orange’]