String to List in Python

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-

  1. Using split()
  2. Using split() with a separator
  3. Using strip()
  4. 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-

  1. In the first step, we have initialized the four strings that we would like to convert.
  2. 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-

  1. In the first step, we have initialized the two strings that we would like to convert.
  2. 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-

  1. In the first step, we have initialized the two strings that we would like to convert.
  2. After this, we have used the split() method followed by map() so that we map the list functionality to each element of the string.
  3. 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.


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263366.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论