

#typecasting the individual elements of the string list into integer using the map() method

Print("Converted string to list : ",list1) #converting the string into list of strings Print("Actual String containing integers: ",string1) Let us look at how we can convert the such type of string to a list in Python. Note, mass type-casting was performed using the map() functionĪ CSV( Comma Separated Values) string, as its name suggests is a string consisting of values or data separated by commas. As we saw in our previous case this gives us a list consisting of character lists.
#Convert string to list code#
Take a look at the given example code below. What if we need a list of characters present in a string? In that case, direct type conversion from string to list in Python using the list() method does the job for us.Ĭertainly, if the input string is something like “abcd”, typecasting the string into a list using the list() method gives us a list having the individual characters ‘a’, ‘b’, ‘c’, ‘d’ as its elements.

type() gives us the type of object passed to the method, which in our case was a string.We consider a string, string1="Python is great" and try to convert the same list of the constituent strings.Print("String converted to list :",string1.split()) Let us look at an example to understand it better. When we need to convert a string to a list in Python containing the constituent strings of the parent string (previously separated by some separator like ‘,’ or space), we use this method to accomplish the task.įor example, say we have a string “Python is great”, and we want a list that would contain only the given names previously separated by spaces, we can get the required list just by splitting the string into parts based on the position of space. Now we are going to discuss each one of the above-mentioned techniques one by one. A string consisting of Integers to a List of integers.Here in this tutorial, we are going to deal with all the methods using which we can convert a string to a list in Python for different cases. It can be achieved by following different methods as per our requirements. Methods of converting a string to a list in PythonĬonversion of a string to a list in Python is a pretty easy job.
#Convert string to list how to#
So, here in this tutorial, we will learn how to convert a string into a list in Python. But, a question arises here, how can we convert a string to different forms of lists? While programming we may need to convert a string to a list in Python.
