The ‘split()‘ method in Python can be used to split a string into a list of substrings based on a delimiter.
1. Split a string into a list of words (where the default delimiter is whitespace):
my_string = "Hello world, enjoy the ride!"
my_list = my_string.split()
print(my_list)
The result:
['Hello', 'world,', 'enjoy', 'the', 'ride!']
2. Split a string based on a specific delimiter, for example the “comma” delimiter:
my_string = "apple,grape,mango,peach,lemon"
my_list = my_string.split(",")
print(my_list)
The result:
['apple', 'grape', 'mango', 'peach', 'lemon']
3. Specify the maximum number of splits (for example, 2). Note that the default is “-1” to get all instances.
my_string = "apple grape mango peach lemon"
my_list = my_string.split(" ", 2)
print(my_list)
The result:
['apple', 'grape', 'mango peach lemon']
Create a function
Here is a function to split a string into a list:
def split_string(my_string, delimiter, max_splits=-1):
"""
Function to split a string into a list.
:param my_string: The input string that you wish to split into a list.
:param delimiter: The specified delimiter to split the string.
:param max_splits: The maximum number of splits, where the default is -1 for all instances.
:return: A list.
"""
if not my_string:
raise ValueError("String is empty")
else:
return my_string.split(delimiter, max_splits)
example_string = "apple,grape,mango,peach,lemon"
print(split_string(my_string=example_string, delimiter=",", max_splits=-1))
The result:
['apple', 'grape', 'mango', 'peach', 'lemon']
Add a Test
To test the “split_string” function using Pytest:
import pytest
# Assuming the split_string function is stored under app.string_operations
from app.string_operations import split_string
@pytest.mark.parametrize(
"input_string, delimiter, max_splits, expected_output",
[
("Hello world, enjoy the ride!", " ", -1, ['Hello', 'world,', 'enjoy', 'the', 'ride!']),
("apple,grape,mango,peach,lemon", ",", -1, ['apple', 'grape', 'mango', 'peach', 'lemon']),
("apple grape mango peach lemon", " ", 2, ['apple', 'grape', 'mango peach lemon']),
("", " ", -1, None)
]
)
def test_split_string(input_string, delimiter, max_splits, expected_output):
"""
Test the split_string function across different strings.
:param input_string: The input string to be tested.
:param delimiter: The delimiter used to split the string.
:param max_splits: The maximum number of splits needed.
:param expected_output: The expected list.
"""
if not input_string:
with pytest.raises(ValueError):
split_string(input_string, delimiter, max_splits)
else:
assert split_string(input_string, delimiter, max_splits) == expected_output
The result:
============================= test session starts =============================
collecting ... collected 4 items
test_string_operations.py::test_split_string[Hello world, enjoy the ride!- --1-expected_output0] PASSED [ '25%']
test_string_operations.py::test_split_string[apple,grape,mango,peach,lemon-,--1-expected_output1] PASSED [ '50%']
test_string_operations.py::test_split_string[apple grape mango peach lemon- -2-expected_output2] PASSED [ '75%']
test_string_operations.py::test_split_string[- --1-None] PASSED ['100%']
============================== 4 passed in '0.03s' ==============================