String conversion is one of the key aspects you will encounter when coding. There can be a million reasons for that. Strings can be converted. You can convert string to lists, list of characters, or even a string that includes integers to a list. In this article, we will be focusing on converting a string to a list in Python.

When I say you can easily do this and that, probably a good question will arise – “But How”?.

We will now discuss each of the above-mentioned methods with examples to make things clear and easier for you.

1. Convert String to List of Strings

You can use the split() function to split the string and store them in a list, which is your end goal. Here we use delimiter as the string to split the input into a list of strings.

Let’s see how it works!

my_string = 'Join Us Here At HackAnons'
my_list = list(my_string.split(' '))
my_list
['Join', 'Us', 'Here', 'At', 'HackAnons']

Here,

  • We have given an input string as “Join Us Here At HackAnons”
  • Then, we have applied split() function to split the string.
  • We used the delimiter as ” ” to split the strings.
  • The split function will split the string based on separator and returns a list as shown above.

Wait, we can do this by defining a function as well. It will be interesting if we do so. Let’s roll!

def convert(string):
    my_list = list(string.split(" "))
    return my_list

my_string = "Join Us Here At HackAnons"
print(convert(my_string))
['Join', 'Us', 'Here', 'At', 'HackAnons']

Perfect!. We have defined a function and later we have passed our input string to the function. It splits the strings and stores them as a new list.

2. Convert string to list of characters

In this section, we are going to convert the input string to characters. Unlike above, we are not using a split() function for this process.

Let’s see how it works!

my_string = 'HackAnons'
my_list = list(my_string)
my_list
['H', 'a', 'c', 'k', 'A', 'n', 'o', 'n', 's']

As simple as that. If you pass any input string a list command, it will return all the characters in a string as a list of characters.

So, the next step will be using a defined function. Yes, now we are going to define a function that returns all the characters in a string as a list.

Let’s see how it works!

def convert(string):
    my_list = list(string)
    return my_list

my_string = "HackAnons"
print(convert(my_string))
['H', 'a', 'c', 'k', 'A', 'n', 'o', 'n', 's']

It is always good to know about interpreting a problem in multiple ways. Same as above, define a function, pass the string to list command and it will return all the characters in the string as a list.

3. Convert String to list of lists

The headline itself is interesting, isn’t it?.

It is just like combining the above methods. we are combing both list of strings and list of characters.

my_string = 'I Love HackAnons'
my_string = my_string.split()
my_list = list(map(list,my_string))
my_list
[['I'], ['L', 'o', 'v', 'e'], ['H', 'a', 'c', 'k', 'A', 'n', 'o', 'n', 's']]

Here,

  • First, we have assigned an input string to a variable.
  • Then we used split() function to split the string.
  • After we have mapped the split strings into list of list.
  • Printed the output.

Note: Be familiar with string formatting to avoid the possible errors.

Ending note

String formatting is one of the most important concepts which assist you more frequently in programming. It can be an analysis or visualization, if you are using strings and want to break them down, then this process will help you a lot.

As illustrated in this article you can convert string to list in python. You can convert string to characters and string to a list of lists as well. That’s all for now. Happy Python!!!

More read: Python Split