How to convert string to list in python?
Conversion of a string to list in Python is a pretty easy job.
We can convert string into list by using split().
e.g.,
s1="python is a famous programming language" print(s1.split())
output - ['python', 'is', 'a', 'famous', 'programming', 'language']
We’ll see another example in which we convert string into a list of characters.
e.g.,
s1="python" print(list(s1))
output - ['p', 'y', 't', 'h', 'o', 'n']
Now we are going to convert a string consisting of only integers separated by some space, comma or etc., to a list with integer type elements.
e.g.,
a="1 2 3 4 5 6" l=list(a.split()) print(l) l1=list(map(int, l)) print(l1)
output - ['1', '2', '3', '4', '5', '6'] [1, 2, 3, 4, 5, 6]
This is how we can convert string to a list in python.
Subscribe
Login
Please login to comment
0 Discussion