Skip to main content

Convert String To Array in Python

In our previous Python tutorial, you have learned how to convert string into Bytes in Python. In this tutorial, You will learn how to convert string to an array in Python.

String is the most used data type in any programming language. You can convert Strings into different data such as Bytes, JSON, Array, list etc. So here int this tutorial, we will expalin how to convert String into array using Python.

As in Python, Array is represented by List and we can assume list as an array. So, basicaly we will work on the list to convert string into list. Python has built-in split() and list() method to convert string into array or list.

So let’s proceed with coding:

Convert String To Array or List

We will convert string to arry using split() method. The method takes a parameter as separator to convert into list. The default separator is any whitespace.

Here is the example code to convert string into list:

myString = "Web, Damn, Programming, Blog" 

myArray = myString.split(",")

print(myArray)

Output:

['Web', 'Damn', 'Programming', 'Blog'] 

Convert String To Array Of Characters

We can also convert String into array of characters using Python built-in method list(). The method also assume whitespaces as character. So you convert string into array of characters, the whitespaces also converted to characters.

Here is the example code to convert string into array of characters:

myString = "WebDamn" 

myArray = list(myString)

print(myArray)

Output:

['W', 'e', 'b', 'D', 'a', 'm', 'n']