Convert String to List python

In this post, we will see how to convert String to list in Python.

We can use String’s split function to convert String to list.


String’s split function

Python String split function signature

Parameters

sep: A string parameter that will be used as a separator.
maxsplit: Number of times split

Let’s see some examples.


Using split without any parameter

if we don’t provide any separator, string will be splitted by space by default.

Output:

Hello world from Java2blog
List of words: [‘Hello’, ‘world’, ‘from’, ‘Java2blog’]

Using split with separator comma

Output:

India,China,Nepal,Bhutan
List of countries: [‘India’, ‘China’, ‘Nepal’, ‘Bhutan’]

Convert string to list of characters

We can convert string to list of characters using list function.

Output:

Hello world
List of words: [‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘ ‘, ‘w’, ‘o’, ‘r’, ‘l’, ‘d’]

That’s all about how to convert string to list python

Was this post helpful?

Leave a Reply

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