given_string.split('sep', 1) to split the string at the first occurrence of a given delimiter. Another approach to solve the problem is to use
text = 'Fred fed Ted bread and Ted fed Fred bread' # Method 1 print(text.split('fed', 1)) # OUTPUT: ['Fred ', ' Ted bread and Ted fed Fred bread'] # Method 2 li = list(text.partition('fed')) li.remove(li) print(li) # OUTPUT: ['Fred ', ' Ted bread and Ted fed Fred bread']
📜Problem: Given a string. How will you split the string at the first occurrence of a specific separator?
Consider that you are given a string with multiple occurrences of the word “scream”. You have to split the string at the first occurrence of the word “scream”.
# Input: text = 'I scream you scream we all scream for ice cream' # Expected Output: ['I ', ' you scream we all scream for ice cream']
Let’s dive into the different ways of solving the given problem.
Method 1: Using split
split() function splits the string at a given separator and returns a split list of substrings. If an additional/optional maxsplit parameter is specified, at most maxsplit splits are done (thus, the list will have at most
maxsplit+1 elements). If maxsplit is not specified or
-1, then there is no limit on the number of splits (all possible splits are made).
🌎Read more here: Python String split()
Approach: As we have to split the string only once at the first occurrence of the given separator, we can use the
maxsplit argument to solve the given problem by setting the
maxsplit = 1. Therefore, the string will split along the specified separator only once from the left end.
text = 'I scream you scream we all scream for ice cream' print(text.split('scream', 1)) # OUTPUT: ['I ', ' you scream we all scream for ice cream']
Method 2: Using partition
partition() method Searches for a separator substring and returns a tuple with three strings: (1) everything before the separator, (2) the separator itself, and (3) everything after it. For example:
finxterx123'.rpartition('x') will return the following tuple:
('finxter', 'x', '123')
🌎Read more here: Python String partition()
li = list(text.partition('scream')) li.remove(li) print(li) # OUTPUT: ['I scream, you scream, we all ', ' for ice cream']
Explanation: Here, we have used the string “
scream” as the separator. The
partition() method splits the string when it finds the ‘scream’ separator. Now, a problem here is the word “scream” is also stored as an item within the list. This is all right if you want to keep the separator. However, in this case we do not need the separator. Hence, we use the
remove() method to eliminate the second element (at index 1) where the separator is stored.
I hope the methods to solve the problem used in this tutorial helped you to learn how to split a string at the first occurrence of a specified delimiter. Please subscribe and stay tuned for more interesting tutorials and solutions.
🌎Recommended Read: Python Split String at Last Occurrence
But before we move on, I’m excited to present you my new Python book Python One-Liners (Amazon Link).
If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!
The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).