How to Split a String into an Array in Python?

In this tutorial, I will explain how to split a string into an array in Python, a common task developers often require. I recently needed to process a large dataset of customer reviews while working on a data analysis project for a client based in New York. Each review was a long string, and I had to break it down into individual words for sentiment analysis. This tutorial will help you understand various methods of splitting strings in Python with examples.

To split a string into an array in Python, use the split() method, which divides the string based on a specified delimiter, defaulting to whitespace. For example, string.split(',') will split the string at each comma, returning a list of substrings. For more complex patterns, use the re.split() method from the re module.

Read How to Remove Elements from an Array in Python?

Split String into Array in Python

Splitting a string into an array (or list) in Python allows you to break down a string into smaller, more manageable parts. This can be particularly useful for text processing, data parsing, and more tasks.

Python provides different methods to split a string into an array. Let me show you each method with examples.

Check out Save an Array to a File in Python

Method 1: Using the split() Method

The most straightforward way to split a string in Python is by using the split() method. By default, this method splits the string by whitespace (spaces, tabs, newlines).

Let me show you an example.

Example:

review = "The food at Joe's Diner in Chicago was absolutely amazing"
words = review.split()
print(words)

Output:

['The', 'food', 'at', "Joe's", 'Diner', 'in', 'Chicago', 'was', 'absolutely', 'amazing']

In this example, the split() method breaks the string into individual words, creating a list of words.

I executed the above Python code using VS code, and you can see the exact output in the screenshot below:

python split string into array

Check out How to Split a String into Equal Parts in Python?

Method 2: Splitting by a Specific Delimiter

Sometimes, you may need to split a string by a specific delimiter in Python, such as a comma, semicolon, or hyphen. You can pass the delimiter as an argument to the split() method.

Example:

Here is an example.

addresses = "123 Main St, New York; 456 Elm St, Los Angeles; 789 Oak St, Miami"
address_list = addresses.split('; ')
print(address_list)

Output:

['123 Main St, New York', '456 Elm St, Los Angeles', '789 Oak St, Miami']

Here, the string is split into a list of addresses using the semicolon and space (;) as the delimiter.

Check out How to Split a String by Index in Python?

Method 3: Using re.split() for Advanced Splitting

For more complex splitting requirements in Python, the re module (regular expressions) offers the re.split() method. This method allows you to split a string based on a regular expression pattern.

Let me show you an example.

Example:

import re

data = "John-Doe:1234|Jane-Smith:5678|Bob-Jones:91011"
pattern = r'[-|:]'
result = re.split(pattern, data)
print(result)

Output:

['John', 'Doe', '1234', 'Jane', 'Smith', '5678', 'Bob', 'Jones', '91011']

In this example, the string is split based on multiple delimiters (hyphen, pipe, and colon) using a regular expression pattern.

Here is the exact output in the screenshot below:

split string into array python

Check out Convert String to List in Python Without Using Split

Method 4: Splitting into a Fixed Number of Parts

If you want to split a string into a fixed number of parts, you can use the maxsplit parameter with the split() method in Python. This parameter specifies the maximum number of splits to perform.

Example:

Here is an example.

info = "Michael Jordan, Basketball, Chicago Bulls, 23"
parts = info.split(', ', 2)
print(parts)

Output:

['Michael Jordan', 'Basketball', 'Chicago Bulls, 23']

In this example, the string is split into three parts, with the last part containing the remaining string.

Conclusion

In this tutorial, I explained how to split a string into an array in Python using different methods with examples. You can use the split() method for simple cases, while re.split() is ideal for more complex scenarios. Additionally, the maxsplit parameter offers control over the number of splits, making it a valuable option for specific requirements in Python.

Feel free to experiment with these methods in your projects, and you’ll find that splitting strings in Python is very easy. Do let me know in the comment below if this tutorial helps.

You may also like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.