How To Change A String Into A List
Introduction
While programming nosotros may need to convert a string to list in Python. That could be for any other reason. Simply, a question arises here, how can we convert a string to different forms of lists?
And so, here in this tutorial, nosotros are going to learn how we tin can convert a string into a list in Python.
Methods of converting a string to listing in Python
Conversion of a cord to listing in Python is a pretty like shooting fish in a barrel task. It can be achieved by post-obit different methods every bit per our own requirements.
Here in this tutorial, we are going to deal with all the methods using which we can convert a string to listing in Python for unlike cases. Below we have listed all the methods:
- String to List of Strings
- String to List of Characters
- List of Strings to Listing of Lists
- CSV to Listing
- A string consisting of Integers to Listing of integers
At present we are going to discuss each 1 of the above-mentioned techniques ane-by-one.
ane. String to List of Strings
When we need to catechumen a string to listing 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 chore.
For example, say we have a string "Python is not bad", and we desire a list which would contain only the given names previously separated by spaces, we tin can get the required list just past splitting the string into parts on the basis of the position of space.
Let us await at an example to understand it improve.
#given cord string1="Python is smashing" #press the cord print("Actual String: ",string1) #gives us the type of string1 print("Blazon of cord: ",type(string1)) print("String coverted to listing :",string1.split()) #prints the list given by split()
Output:
In the higher up code:
- Nosotros consider a string,
string1="Python is great"
and attempt to convert the same a list of the elective strings -
type()
gives usa the type of object passed to the method, which in our instance was a string -
divide()
is basically used to split a string into a listing on the footing of the given separator. In our code, the words were separated by spaces. By default, if we do not pass anything to the split() method it splits upwards the string on the basis of the position of spaces - Hence though we have not mentioned the separator parameter, the
split()
method gives us a list of the respective strings
2. String to List of Characters
What if we need a listing of characters present in a string? In that example, directly type conversion from string to listing in Python using the list()
method does the chore for united states of america.
Certainly, if the input string is something like "abcd", typecasting the string into a listing using the list()
method gives u.s. a list having the individual characters 'a', 'b', 'c', 'd' as its elements. Take a wait at the given example code beneath.
#given string string1="AskPython" #press the string impress("Bodily String: ",string1) #confirming the type() print("Type of string: ",blazon(string1)) #type-casting the cord into list using listing() print("Cord coverted to listing :\n",list(string1))
Output:
Understanding the code:
- Firstly hither, we initialize a string,
string1
as "AskPython" and print its type using theblazon()
method - And as nosotros can observe, typecasting the string using the
listing()
method gives us a list of the member characters, equally required
3. Listing of Strings to Listing of Lists
Here, we are going to run into how nosotros can combine both the to a higher place methods to convert a string to a list of character lists.
Look at the below-given example carefully,
#Given string string1="This is Python" print("The actual string:",string1) #converting string1 into a list of strings string1=string1.split() #applying listing method to the individual elements of the list string1 list1=listing(map(list,string1)) #press the resultant list of lists print("Converted to list of character list :\n",list1)
Output:
Understand the lawmaking:
- In this example, afterward the initialization of the string
string1
, nosotros use the beginning method and catechumen it into a list of strings - That is, at this point string1 is a listing of strings given past
[ 'This', 'is', 'Python' ]
- Then nosotros apply the
list()
method to all the elements of the listing - string1. As we saw in our previous example this gives us a list consisting of grapheme lists. Note, mass blazon-casting was performed using the map() office
4. CSV to Listing
A CSV( Comma Separated Values) string, every bit its name suggests is a string consisting of values or information separated past commas.
Let us wait at how we can convert such type of cord to listing in Python.
#given string string1="abc,def,ghi" print("Actual CSV String: ",string1) print("Blazon of string: ",blazon(string1)) #spliting string1 into list with ',' as the parameter print("CSV coverted to list :",string1.carve up(','))
Output:
Here:
- Similarly, we initiate by because a string string1 with various information or values separated by commas(',')
- After printing it and its
blazon()
, we go on by splitting it on the basis of the parameter ',' - This makes the values 'abc', 'def', and 'ghi' the elements of a list. In this way, we were actually able to extract values from a given CSV
5. A string consisting of Integers to List of integers
Now we are going to convert a string consisting of only integers separated past some space, comma or etc., to a listing with integer type elements.
For case, look at the code below,
#string with integers sepated by spaces string1="1 2 3 4 5 vi seven 8" impress("Bodily String containing integers: ",string1) print("Type of string: ",type(string1)) #coverting the cord into list of strings list1=list(string1.dissever()) print("Converted string to list : ",list1) #typecasting the individual elements of the string list into integer using the map() method list2=list(map(int,list1)) print("Listing of integers : ",list2)
Output:
Now:
- We took a string,
string1
equally "i ii three iv five vi seven 8" and print it and its type() consecutively - Then we split it using the
split()
method and shop the resultant list into a listing, list1. At this bespeak, list1 holds [ 'one', 'two' , '3', '4', '5', '6', 'seven', '8' ] equally nosotros can see from the output, equally expected - Now we map the role
int()
throughout the list, typecasting each 1 of the elements into integers. And farther, we store the typecasted mapped list into list2 and print the same - Every bit a result, we go a list consisting of the integer elements on which now we can perform arithmetic operations.
Conclusion
That's all now, this was all well-nigh converting strings into different lists using various methods. Endeavour to use the i which suits your lawmaking and solves your purpose as well equally meets upward to your requirements. Questions in the comments are appreciated.
References
- https://world wide web.askpython.com/python/cord
Source: https://www.askpython.com/python/string/convert-string-to-list-in-python
Posted by: upchurchyouren.blogspot.com
0 Response to "How To Change A String Into A List"
Post a Comment