Convert String To A List In Python

In this tutorial, you will learn to convert String to a List in Python. You can convert a string in to list using the typecast method, and using the split() function.

In this tutorial, I will also show you the examples codes for all below conversion that I am going to cover.

How To Convert String To A List In Python With Split

Python’s split function is the first way you can turn a string into a list of words. In Python, we use this method when we want to turn a string into a list with all of the separate strings that make up the parent string (like “,” or “.”).

For example, if we have a string that says “You are On Coduber” and we want a list that only has the names of those names separated by spaces, we can just split the string into parts based on where the space is.

Let us see in the below example code the usage of split function for converting string to list of words.

#Initializing the String to Convert
#To The List
newSentence = "You are On Coduber"

#Using Split Function To Make It List
#With Spaces
listOfWords = newSentence.split(" ")

#Printing The List Of Words
print(listOfWords)

Output:

['You', 'are', 'On', 'Coduber']

Explanation:

  • Created a new variable to store the string sentence that contains spaces.
  • Now created the new variable that stores the list of words when used the split function to split the sentence based upon the spaces.
  • Hence whenever split function finds a new spaces it store the previous word in a list.
  • And then in last I printed the list of words that we got from the string as a list.
Convert String To A List In Python

How To Convert String To List In Python Without Split

You can convert a string to a list without using the split function by using the typecast method. You only need to typecast the string variable to list. It should be noted that in this method, each word in a string is treated as a separate element in the list.

Let us see the below code example of conversion of string without using split in Python.

#Initializing the String to Convert
#To The List
newSentence = "You are On Coduber"

#Converting String to list Without Split
stringToList = list(newSentence)

#Printing The Converted List
print(stringToList)

Output:

['Y', 'o', 'u', ' ', 'a', 'r', 'e', ' ', 'O', 'n', ' ', 'C', 'o', 'd', 'u', 'b', 'e', 'r']
Convert String To A List In Python

How To Convert String List To Int List In Python

If you have a list that has a string that only contains integer numbers with spaces and wants to convert it to a list of integers, you can do so by using list comprehension and typecasting it with integer and then adding it to the new list.

Let us see in the below code example how you can achieve this with String List To Int List.

#Initializing the String to Convert
#To The List
newSentence = ['1', '2', '3', '4', '5']

#Using List Comprehension
listNumber = [int(i) for i in newSentence]

#Printing The Converted List
print(listNumber)

Output:

[1, 2, 3, 4, 5]
Convert String To A List In Python

How To Convert Comma Separated String To List In Python

In Python, if you have a String that contains a comma-separated string and wants to convert it to a list, you can use the split function. Using the split function, you can separate the string from each comma and then add it to the list.

Let us see in the below code example how to convert comma-separated string to list in Python.

#Initializing the String to Convert
#To The List
newSentence = "You,Are,On,Coduber"

#Using Split Function
listCommasSep = newSentence.split(",")

#Printing The Converted List
print(listCommasSep)

Output:

['You', 'Are', 'On', 'Coduber']
Convert String To A List In Python

How To Convert String To List Of Characters In Python

It is possible to convert a string to a list of characters using a simple typecast operation. To do so, typecast a string into a list and then assign the result of the typecast to a list variable

Let’s take a look at an example of typecasting a string to get a list of characters in the code below.

#Initializing the String to Convert
#To The List
newSentence = "You Are On Coduber"

#Using Split Function
listOfChar = list(newSentence)

#Printing The Converted List
print(listOfChar)

Output:

['Y', 'o', 'u', ' ', 'A', 'r', 'e', ' ', 'O', 'n', ' ', 'C', 'o', 'd', 'u', 'b', 'e', 'r']

Wrap Up

I hope you were able to grasp the concepts behind all of the Python code examples dealing with the conversion of a string to a list. In this tutorial, I attempted to cover all of the possible conversions that can occur from string to list in the Python programming language.

Please let me know in the comments section if you require code for any specific code conversion or if I have overlooked something and I will be happy to include it.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Python User Input from Keyboard – input() function
  2. Fix: Python Is Not Set From Command Line Or npm Configuration
  3. How To Take Integer Input In Python 3
  4. How To Compare String and Integer in Python
  5. How To Print Fractional Part In Python

Leave a Comment