In this blog post, we will be discussing how to split strings in Python. We will detail how to use the split methods in python as well as some complex examples of how these two methods can be used.

Strings are a fundamental part of programming languages like Python; it is important that you know how they work!

How to split a string in python example

How to Split a String in Python

Python has two methods for splitting strings: the split() method and the rsplit() method. Both of these are string methods. The first method, split(), takes a given string and then splits it into an array of substrings. The second method, rsplit(), reverses how this is done; it starts splitting the string from the right and moves to the left until it reaches the beginning of the python string.

Using the split() Method

The split() method splits a string into a list of strings based on a separator. The separator can be any character, including whitespace. The default value of the separator is white space. If the separator is not found in the python string, then the method returns a single string.

Split() Method Examples

Let’s look at how to use the split() method in Python:

# import the split package
from string import split<

# Create a string in python
fruit_string = "I like to eat apples, oranges, and pears"

# Print out the string for reference
print('Original fruit_string: ' + fruit_string)

# Split the string by spaces and print out the resulting array
print('split WS Max: Empty')
print(split(fruit_string))

Result:

Original fruit_string: I like to eat apples, oranges, and pears
split WS Max: Empty
['I', 'like', 'to', 'eat', 'apples,', 'oranges,', 'and', 'pears']

In this example we are splitting the string, “I like to eat apples, oranges, and pears”, into multiple strings and printing out the resulting array. You can see the split string is 8 entries long, which is the maximum number of splits.

We can use python split in another way. We can use it as a method of the string as in the following example:

print(fruit_string.split()

Give it a try yourself. You should get the same result.

Using split() with other delimiters:

from string import split
fruit_string = "I like to eat apples, oranges, and pears"
print(fruit_string)

# Here we have a specified separator of a ,
print('split Comma Max: Empty')
print(split(fruit_string, ','))

Result:

Original fruit_string: I like to eat apples, oranges, and pears
split Comma Max: Empty
['I like to eat apples', ' oranges', ' and pears']

Using the rsplit() Method

The rsplit() method splits a string into a list of strings based on a delimiter. The delimiter can be any character, including whitespace. If the delimiter is not found in the string, then it returns an empty list.

rsplit() Method Examples

Let’s look at how to use the rsplit() method in Python:

from string import rsplit
# Create the split string

fruit_string = "I like to eat apples, oranges, and pears"
print(fruit_string)

print('rsplit WS Max: Empty')
print(rsplit(fruit_string))

Result:

Original fruit_string: I like to eat apples, oranges, and pears
rsplit WS Max: Empty
['I', 'like', 'to', 'eat', 'apples,', 'oranges,', 'and', 'pears']

Once again, we can use rsplit with the alternate syntax:

fruit_string.rsplit()

Using rsplit() with other delimiters:

from string import rsplit
fruit_string = "I like to eat apples, oranges, and pears"
print(fruit_string)

# Here we have a specified separator of a comma
print('rsplit Comma Max: Empty')
print(rsplit(fruit_string, ','))

Result:

Original fruit_string: I like to eat apples, oranges, and pears
rsplit Comma Max: Empty
['I like to eat apples', ' oranges', ' and pears']

Here we see the result is the same as the first split usage. And because we didn’t specify a max value (shown below) the string is split into the maximum number of smaller chunks, the same as using split() without parameters.

Complex Examples Using split() and max syntax

There are times when you need to split strings based on how they’re formatted; this is where using the split() method can be useful! Let’s look at how we could do this:

from string import split
fruit_string = "I like to eat apples, oranges, and pears"
print(fruit_string)

# Here we have a specified separator of a comma and max of 1
print('split WS Max: 1')
print(fruit_string.split(' ', 1))

Result

Original fruit_string: I like to eat apples, oranges, and pears
split WS Max: 1
['I', 'like to eat apples, oranges, and pears']

You can see here the number of splits is lessor in this care, the python split string only occurred at the first space encountered.

Complex Examples Using rsplit() and max syntax

Now let’s look at how we could use the rsplit() method to split strings in different ways, so we specify a max:

from string import rsplit
fruit_string = "I like to eat apples, oranges, and pears"
print(fruit_string)

# Here we have a specified separator of a comma and max of 1
print('rsplit Comma Max: 1')
print(fruit_string.rsplit(',', 1))

Result:

Original fruit_string: I like to eat apples, oranges, and pears
rsplit Comma Max: 1
['I like to eat apples, oranges', ' and pears']

You can see here when we split a string with rsplit, a comma, and a max of 1, we have the python string split starting from the right which is the opposite of when we split a string with split() which started from the left. This results in effectively the first split occurring after oranges instead of with split() where it occurred after apples.

Summary

This blog post has given you some insight into how the string methods; split() and rsplit() work in Python! Hopefully, you will find this useful when you are working on your next project.