String to List in PythonSo 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-
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 second program, we have specified a separator in split(). Using split() with a SeparatorConsider 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-
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-
Finally, in the last program we have used the string of integers, Converting String of IntegersConsider 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. ConclusionIn this tutorial, we learned the simple techniques of converting a string to a list in Python. |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263366.html