How to split a string into a list?

In Python, the string method split() can be used to divide a string into a list based on a specified delimiter.

Example:


  text = 'apple,banana,orange'
  fruits = text.split(',')
  

Solution:

By default, the split() method divides the string at each space. However, you can specify a delimiter, like ',' in this case:


  print(fruits)
  

Beginner's Guide to Python