Table of Contents

2 Ways to convert string to list in Python

The split method is used to split a string based on a specified delimiter. Once split, it returns the split string in a list, using this method we can convert string to list in Python.

Using Split() to convert string to list in Python

The split method is used to split a string based on a specified delimiter. Once split, it returns the split string in a list, using this method we can convert string to list in Python.

str_1 = "Hire the top 1% freelance developers"
list_1 = str_1.split()
print(list_1)
#Output:
['Hire', 'the', 'top', '1%', 'freelance', 'developers']

Using list() to convert string to list in Python

As aforementioned, this method converts a string into a list of characters. Hence this method is not used quite often.

I would recommend using this method only if you are certain that the list should contain each character as an item and if the string contains a set of characters or numbers that are not divided by a space. If not, the spaces would also be considered as a character and stored in a list.

str_1 = "Hire freelance developers"
list_1 = list(str_1.strip(" "))
print(list_1)
#Output:
['H', 'i', 'r', 'e', ' ', 'f', 'r', 'e', 'e', 'l', 'a', 'n', 'c', 'e', ' ', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', 's']

You might also like