python

How To Convert String To List in Python

In this tutorial, we are going to see how to convert string to list in Python. Python provides different types of variables for programmers. We can use data types like int, float, string, list, set… in our applications. When using different types of variables, it may be necessary to convert these to different types.
 

How To Convert String To List in Python

We can convert a string into a list by using the split() function. This method returns a list of strings split by a specified separator.

str = 'Welcome To StackHowTo'
print(str.split())

Output:

['Welcome', 'To', 'StackHowTo']
 

How To Convert String To List of Characters in Python

If you don’t want leading and trailing white space to be part of the list, you can use the strip() function before converting to list.

str = 'xyz'
print(list(str.strip()))

Output:

['x', 'y', 'z']
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *