Convert string to list in Python

In this short tutorial, find how to convert string to list in Python. We look at all the ways you can achieve this along with their pros and cons.

This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often use…


This content originally appeared on DEV Community and was authored by hrishikesh1990

In this short tutorial, find how to convert string to list in Python. We look at all the ways you can achieve this along with their pros and cons.

This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often used or interesting concepts.

Table of Contents

Converting string to list in Python:

Data type conversion or type casting in Python is a very common practice. However, converting string to list in Python is not as simple as converting an int to string or vice versa.

Strings can be converted to lists using list(). We will look at this method below. However, in this method, Python would not know where each item starts and ends, returning a list of characters. Hence, Python provides a few alternate methods which can be used to convert a string to a list.

Solution 1: Using Split()

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.

Syntax:

string.split( delimiter, maxsplit)

Parameters:

  • Delimiter - Optional. Specifies the desired delimiter to use when splitting the string. If left empty, whitespaces are considered delimiters.
  • Maxsplit - Optional. Specifies how many splits to do.

Code to 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']

Note: Since no argument was passed as a delimiter, the string was split on whitespace.

Now let’s take a look at an instance where the delimiter is specified.

str_1 = "Hire-the-top-1%-freelance-developers"

list_1 = str_1.split("-")
print(list_1)

#Output:
#['Hire', 'the', 'top', '1%', 'freelance', 'developers']

Solution 2: Using list():

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.

Code to Convert string to list in Python:

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']

Convert string to list in Python - Closing thoughts

The split() method is the recommended and most common method used to convert string to list in Python. This method does not have any significant cons. On the other hand, the typecast method is not widely recommended and is used only when the requirements are met. However, please feel free to practice both methods to facilitate a better understanding of the concept.

Do let me know your thoughts in the comments section below. :)


This content originally appeared on DEV Community and was authored by hrishikesh1990


Print Share Comment Cite Upload Translate Updates
APA

hrishikesh1990 | Sciencx (2021-08-02T06:36:06+00:00) Convert string to list in Python. Retrieved from https://www.scien.cx/2021/08/02/convert-string-to-list-in-python/

MLA
" » Convert string to list in Python." hrishikesh1990 | Sciencx - Monday August 2, 2021, https://www.scien.cx/2021/08/02/convert-string-to-list-in-python/
HARVARD
hrishikesh1990 | Sciencx Monday August 2, 2021 » Convert string to list in Python., viewed ,<https://www.scien.cx/2021/08/02/convert-string-to-list-in-python/>
VANCOUVER
hrishikesh1990 | Sciencx - » Convert string to list in Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/02/convert-string-to-list-in-python/
CHICAGO
" » Convert string to list in Python." hrishikesh1990 | Sciencx - Accessed . https://www.scien.cx/2021/08/02/convert-string-to-list-in-python/
IEEE
" » Convert string to list in Python." hrishikesh1990 | Sciencx [Online]. Available: https://www.scien.cx/2021/08/02/convert-string-to-list-in-python/. [Accessed: ]
rf:citation
» Convert string to list in Python | hrishikesh1990 | Sciencx | https://www.scien.cx/2021/08/02/convert-string-to-list-in-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.