Do you want to split a string into an array in Python? In this Python tutorial, I have explained various ways to split a string into an array in Python using built-in methods and functions.
There are different ways to split a string into an array in Python.
- Using the
split()
method - Using the
splitlines()
method - Using a list comprehension
- Using the
re.split()
method from there
module
Split a String into an Array in Python using split() method
The split()
method is a built-in string method in Python that allows you to split a string into an array based on a specified delimiter. Here’s how you can use the split()
method with the given example strings:
Example 1 – Splitting a string into an array in Python by whitespace:
string = "United States of America"
delimiter = " " # Use a space as the delimiter
array = string.split(delimiter)
# Output: ['United', 'States', 'of', 'America']
print(array)
In this example, we set the delimiter as a space (” “). The split()
method then splits the string at each occurrence of the delimiter, resulting in a list of substrings.
Example 2 – Splitting a string into an array by a comma and a space in Python:
string = "Los Angeles, Chicago, Indianapolis, Boston"
delimiter = ", " # Use a comma followed by a space as the delimiter
array = string.split(delimiter)
# Output: ['Los Angeles', 'Chicago', 'Indianapolis', 'Boston']
print(array)
In this example, we set the delimiter as a comma followed by a space (“, “). The split()
method splits the string at each occurrence of the delimiter, resulting in an array of city names in Python.
Split a String into an Array in Python using splitlines()
method
We can also use the splitlines() method in Python to split a string into an array in Python.
string = "Line 1\nLine 2\nLine 3"
array = string.splitlines()
# Output: ['Line 1', 'Line 2', 'Line 3']
print(array)
Split a String into an Array using a list comprehension in Python
Now, let us see another example of splitting a string into an array using a list comprehension in Python.
string = "Hello"
array = [char for char in string]
# Output: ['H', 'e', 'l', 'l', 'o']
print(array)
In this case, it will split a string into individual characters in Python.
Split a String into an Array using Python re.split()
method
Now, let us see a few examples of how to split a string into an array in Python using the re.split() method.
The re.split()
method from the re
module allows you to split a string based on a regular expression pattern. This is useful when you need to split a string using more advanced rules or using multiple delimiters.
Example-1
For the string “United States of America”, you might not need the power of regular expressions. However, I’ll provide an example using re.split()
to illustrate how it works:
import re
string = "United States of America"
pattern = " " # Splitting by whitespace (spaces) in this case
array = re.split(pattern, string)
# Output: ['United', 'States', 'of', 'America']
print(array)
In this example, we use a simple regular expression pattern (a space) to split the string in array. re.split()
takes the pattern and the string as arguments, and returns an array substrings after splitting the input string in Python.
Example-2: Splitting on multiple characters
import re
string = "United.States:of/America"
pattern = "[./:]"
array = re.split(pattern, string)
# Output: ['United', 'States', 'of', 'America']
print(array)
Example-3: Splitting on words
import re
string = "apple12orange34banana56grape"
pattern = r"\d+"
array = re.split(pattern, string)
# Output: ['apple', 'orange', 'banana', 'grape']
print(array)
Example-4: Splitting on a combination of characters and patterns
Let us see, how to split a string into arrary in Python, which contains a combination characters and patterns,
import re
string = "Earth, Mars123;Jupiter:Pluto 789"
pattern = r"[\s,;:]\s*|\d+"
array = re.split(pattern, string)
# Output: ['Earth', 'Mars', 'Jupiter', 'Pluto', '']
print(array)
Example-5: Splitting on multiple whitespace characters (spaces, tabs, newlines)
Let us see, another example on how to split a string into array on multiple whitespace characters in Python
import re
string = "United\tStates of\nAmerica"
pattern = r"\s+"
array = re.split(pattern, string)
# Output: ['United', 'States', 'of', 'America']
print(array)
Conclusion
In this Python tutorial, we have explored various methods to split a string into an array (a list) in Python. We started with the simplest and most common technique of using the built-in split()
method, which allows you to split a string based on a specified delimiter. We then looked at the splitlines()
method, which is particularly useful for splitting multiline strings into separate lines.
For more complex string splitting scenarios, we delved into using list comprehensions, which offer greater control over the splitting process, as well as the re.split()
method from the re
module, which uses regular expressions for advanced splitting based on patterns or multiple delimiters.
You may also like the following Python tutorials:
- How to split a string into equal half in Python?
- How to split a string by index in Python
- How to split a string into individual characters in Python
- How to Split the Last Element of a String in Python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.