How to Convert Python String to Array

Introduction

Python does not have a built-in “array” data type; instead, you can convert Python String to List. Therefore, you can assume a Python list as an array in this example.

How to Convert String to Array

Use the string.split() method to convert a string to an array in Python. The string.split() method splits the string from the delimiter and returns the splitter elements as individual list elements.

You can specify the separator; the default separator is any whitespace.

Syntax

string.split(separator, maxsplit)

Parameters

Both parameters are optional.

It takes a separator as an optional parameter used to split the String. By default, whitespace is a separator.

The maxsplit parameter specifies how many splits to do. The default value is -1, which is “all occurrences“.

Example

str = "Millie Bobby Brown is Enola Holmes"

arr = str.split()

print(arr)

Output

['Millie', 'Bobby', 'Brown', 'is', 'Enola', 'Holmes']

In this example, we have not explicitly provided the separator, so it takes whitespace as a separator, splits the String based on that separator, and returns the list.

To split the String at a specific character, use the split() function and pass the specific character in the argument.

str = "Millie,Bobby,Brown,is,Enola,Holmes"

arr = str.split(',')

print(arr)

Output

['Millie', 'Bobby', 'Brown', 'is', 'Enola', 'Holmes']

We split the String at the ‘,’ separator in this example.

How to convert a string to an array of characters

Convert a string to an array of characters using Python’s built-in list() function. When converting a string to an array of characters, whitespaces are also treated as characters.

str = "MillieB11"

arr = list(str)

print(arr)

Output

['M', 'i', 'l', 'l', 'i', 'e', 'B', '1', '1']

In this example, split the String by converting the String to the list (using typecast).

FAQs

What is the difference between a string and an array in Python?

The main difference between a string and an array is that string is a sequence of characters, while an array is a data structure that holds a collection of elements of any data type in Python.

A string is immutable, and an array or list is a mutable data structure.

What are the ways to convert a string to an array in Python?

There are mainly three ways.

  1. Using the split() method.
  2. Using the list() method.
  3. Using the numpy.array() method.

Python does not have a built-in array data structure, but you can create an array using the numpy library’s np.array() function.

Can I convert a list of strings to an array of strings in Python?

Yes, use the np.array() function to convert a list of strings to an array.

Are there any limitations when converting strings to arrays in Python?

Depending on the method used to convert a string to an array and how the string is formatted, there may be limitations on the types of strings that can be converted or the desired output format.

Conclusion

Use the string.split() method to convert a string to a list or array.

Use the list() method to convert a string to a list of characters.

Use the np.array() function to convert a list of strings to an array.

2 thoughts on “How to Convert Python String to Array”

  1. You can use list comprehensions to do str -> char[] as well

    str = ‘abc’
    result = [c for c in str]
    result
    type(result)

    // [‘a’,’b’,’c’]
    // list

    Reply
  2. how to convert a list

    1.2175443743582222
    1.2175624154470546
    1.216812476226702
    1.2135922330097086
    1.2135922330097086
    1.2174474363703431

    to

    [1.2175443743582222,
    1.2175624154470546,
    1.216812476226702,
    1.2135922330097086,
    1.2135922330097086,
    1.2174474363703431]

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.