How To Convert String To Array In Python?

Python does not provide builtin type named array which exists in popular programming languages PHP, C, C++, etc. But Python provides a very similar data type named List. Strings can be converted into the List in Python by using the split() method.

Python String to Array

Python string type provides the split() method which is used to split the current string according to the provided separator and return it as a list. The syntax of the split() method is like below.

STRING.split(SEPARATOR,MAXSPLIT)
  • STRING is the string value or variable which will be returned as list according to the provided separator.
  • SEPARATOR is single or more characters which will be used to split provided STRING.
  • MAXSPLIT is maximum split count which is an optional parameter and generally not used.

In the following example, we split the string “I,like,pythontect” into list or array.

str = "I,like,pythontect"

array = str.split(",")

print(array)
["I","like","pythontect"]

Python String to Character Array

Another string to array conversion way is converting string characters into an array where every character will be an item in the array. The list() method is used to convert given string characters into an array.

str ="pythontect"

array = list(str)
['p','y','t','h','o','n','t','e','c','t']

Leave a Comment