Python Convert String To List

Python provides the String and List data types which can be used to store multiple values. The string is used to store multiple characters. The list is used to store multiple items like strings, number,s etc. We can convert strings to lists easily by using the split() method.

String To List

The split() method of the string type can be used to convert string to list. By default, the spaces are used as delimiters for the string. This means a string splitter as a list.

s = "I like linuxtect"
print(s.split())
['I', 'like', 'linuxtect']

Convert String To List with Comma Separator

By default the split() method uses space as the separator but we can use a different separator using the comma separator. In the following example, we convert a string into a list by using a comma as a separator.

s = "I,like,linuxtect"
print(s.split(","))
['I', 'like', 'linuxtect']

Leave a Comment