There are many ways we can use to convert a string into a list in Python. In this post, we’ll be covering two methods: the split() method and the list() method, and with two practical examples: Let’s get started!
Method 1: converting a phrase into words using the split() method
phrase = 'how to create a list from a string in python' # here we are using "space" as a delimiter. You can use any delimiter you want words = phrase.split(' ') print(words) # ['how', 'to', 'create', 'a', 'list', 'from', 'a', 'string', 'in', 'python']
Method 2: converting a word into characters using the list() method
word = 'python' characters = list(word) print(characters) # ['p', 'y', 't', 'h', 'o', 'n']
Converting a string into a list in python is easy. We’ve handled two use cases with the help of split() and list() methods, but you can use these two methods to deal with any other use case you may encounter.