Python Split String to Array

In this Python tutorial, you will learn about Python split string to an array using different methods.

I generally use the concept of splitting string to the array while tokenizing the text data for natural language processing, where words need to be separated from each other based on the delimiter.

In this tutorial, I have explained how to split any string into an array using two approaches. You may be familiar with them, but going through those approaches will clear your understanding of how to split the string.

Let’s start,

Python Split String to Array

In Python, there are multiple approaches to splitting the string into an array, and I will explain the most commonly used approaches.

But first, let me decode the concepts of splitting strings into arrays. For example, if you have a string like “You are the 1900s king”, you can split it word by word or character by character into an array. Here, array means a list in Python.

Remember, a string is a collection of sequences of characters. Your string can be a single word, sentence or paragraph. you can split this string into a Python list (which is called an array in another programming language)

Python String to Array using the Split() Function

The split() method in Python takes a parameter called delimiter. Based on this delimiter, a string is divided into substrings. Here, you will use this concept to split the string into an array.

READ:  Find Smallest Number in Python using For Loop

The syntax is given below.

str.split(delimiter)

Where,

  • str: It is the string that you want to split into an array.
  • split(delimiter): It is the function split(), which takes a delimiter or string separator on which the string is divided into an array.

Let’s take an example: suppose you have strings like those shown below.

str = "You are the 1900s king"

To split the above string into an array, you can use the spit() method with delimiter as space as shown below.

string_to_array = str.split(" ")

print(string_to_array)
Python String to Array using the Split() Function

From the output, you can see that the string “You are the 1900s king” is split into the array as ‘[‘You’, ‘are’, ‘the’, ‘1900s’, ‘king’]’. Each part of the original string becomes an element of the list or array.

If you look closely at the string, each word is separated by a space (white space), which is called a delimiter. So, in this line of code, ‘string_to_array = str.split(” “)’, the split(” “) method is called on the string ‘str’ with the delimiter ” “, which represents the space.

The split(” “) method splits all the strings in the list based on the specified delimiter ” ” and stores them in the new variable string_to_array as a list.

Split String in Array Python using re Module

The re module in Python has a function called split(), or you can access this method from the re module like this: re.split(). This method allows you to split the string based on the specified regular expression (pattern).

If you have the string “United States of America”, for example, you might not need the power of regular expressions. However, I’ll provide an example using re.split() to illustrate how it works.

READ:  How to Indent Multiple Lines in Python

First, import the re-module.

import re

Then, declare the string that you want to split into an array, as shown below.

string = "United States of America"

After that, create a pattern and pass this pattern to the re.split() function to split the string into an array, as shown below.

pattern = " " 
array = re.split(pattern, string)
print(array)
Split String in Array Python using re Module

From the output, you can see that the string “United States of America” is converted into an array or list like this: “[‘United’, ‘States’, ‘of’, ‘America’]”.

Here, this line of code, re.split(pattern, string), takes the pattern, which is the space represented by ” “, and the string that you want to split into an array or list. As a result, the re.split() method splits the string into an array and stores it in the new variable called ‘array’.

This is how to use Python’s re.split() function to split a string into an array (list).

Conclusion

In this Python tutorial, you learned about Python splitting string to an array using the split() and re.split() methods of Python.

You may like to read: