String to List in Python
So far, we have discussed various conversions in Python. In this tutorial, we will learn another one, which is converting a string to a list in Python.
We will use the following methods to meet our objective-
- Using split()
- Using split() with a separator
- Using strip()
- Using map()
Let us discuss each one of them.
In the first program, we will make use of split() to convert the string to a list in Python.
Using split()
The program given below illustrates how it can be done.
Output:
['Let', 'us', 'study', 'programming.'] ['But', 'before', 'that', 'it', 'is', 'essential', 'to', 'have', 'a', 'basic', 'knowledge', 'of', 'computers.'] ['So', 'first', 'study', 'what', 'is', 'IPO', 'cycle.'] ['Then', 'learn', 'about', 'the', 'generation', 'of', 'computers.']
Explanation-
- In the first step, we have initialized the four strings that we would like to convert.
- After this, we have used the split() so that we obtain a list in which each word of a string represents an element of the list.
In the second program, we have specified a separator in split().
Using split() with a Separator
Consider the given program,
Output:
['Let ', ' us ', ' study ', ' programming.'] ['But ', ' before ', ' that ', ' it ', ' is ', ' essential ', ' to ', ' have ', ' a ', ' basic ', ' knowledge ', ' of ', ' computers.'] ['So ', ' first ', ' study ', ' what ', ' is ', ' IPO ', ' cycle.'] ['Then ', ' learn ', ' about ', ' the ', ' generation ', ' of ', ' computers.']
Explanation-
The approach is similar to the previous program, the only difference it takes an element in the list whenever a separator occurs.
In this program, the separators in the strings were @, #, $ & %.
Now, let’s see how to strip() can be used.
Using strip()
The following program illustrates the same-
Output:
['L', 'e', 't', ' ', 'u', 's', ' ', 's', 't', 'u', 'd', 'y', ' ', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g', '.'] ['B', 'u', 't', ' ', 'b', 'e', 'f', 'o', 'r', 'e', ' ', 't', 'h', 'a', 't', ' ', 'i', 't', ' ', 'i', 's', ' ', 'e', 's', 's', 'e', 'n', 't', 'i', 'a', 'l', ' ', 't', 'o', ' ', 'h', 'a', 'v', 'e', ' ', 'a', ' ', 'b', 'a', 's', 'i', 'c', ' ', 'k', 'n', 'o', 'w', 'l', 'e', 'd', 'g', 'e', ' ', 'o', 'f', ' ', 'c', 'o', 'm', 'p', 'u', 't', 'e', 'r', 's', '.']
Explanation-
- In the first step, we have initialized the two strings that we would like to convert.
- After this, we have used the strip() so that we obtain a list, where each character represents of the string represents an element in the list.
Convert Strings to List of Lists using map()
Output:
[['L', 'e', 't'], ['u', 's'], ['s', 't', 'u', 'd', 'y'], ['p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g', '.']] [['B', 'u', 't'], ['b', 'e', 'f', 'o', 'r', 'e'], ['t', 'h', 'a', 't'], ['i', 't'], ['i', 's'], ['e', 's', 's', 'e', 'n', 't', 'i', 'a', 'l'], ['t', 'o'], ['h', 'a', 'v', 'e'], ['a'], ['b', 'a', 's', 'i', 'c'], ['k', 'n', 'o', 'w', 'l', 'e', 'd', 'g', 'e'], ['o', 'f'], ['c', 'o', 'm', 'p', 'u', 't', 'e', 'r', 's', '.']]
Explanation-
- In the first step, we have initialized the two strings that we would like to convert.
- After this, we have used the split() method followed by map() so that we map the list functionality to each element of the string.
- On executing the given program, the desired output is displayed.
Finally, in the last program we have used the string of integers,
Converting String of Integers
Consider the program given below,
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9] [12, 21, 32, 44, 54, 76, 83]
Explanation-
The logic is similar to the above program but here we have passed a string of integers and applied the ‘int’ functionality on all the elements.
Conclusion
In this tutorial, we learned the simple techniques of converting a string to a list in Python.