💬 Question: Given a Python string. How to split the string after the k-th occurrence of the separator (string or character)? In other words: how to ignore the first (k-1) separator occurrences when splitting a string?
Here are three examples:
sep='-'should be split to
['a-b', 'c', 'd', 'e', 'f', 'g', 'h']
sep='xxx'should be split to
['helloxxxworldxxxpython', 'is', 'great']
- Border case:
sep='-'should be split to
👉 Related Tutorial: Python Split String After Second Occurrence
You can split a string after the k-th occurrence of a given character in three steps:
- First, split the whole string using the separator sep in
- Second, combine the first k elements of the resulting split list using the
- Third, use slicing and list concatenation to create a new result list.
The following code creates a function that takes as input a string
s, an integer
k, and a separator string
sep and splits the string at the
k-th occurrence of the separator:
def my_split(s, k, sep): all_split = s.split(sep) return [sep.join(all_split[0:k])] + all_split[k:] print(my_split('a-b-c-d-e-f-g-h', k=2, sep='-')) # ['a-b', 'c', 'd', 'e', 'f', 'g', 'h'] print(my_split('helloxxxworldxxxpythonxxxisxxxgreat', k=3, sep='xxx')) # ['helloxxxworldxxxpython', 'is', 'great'] print(my_split('a-b', k=100, sep='-')) # ['a-b']
The code does multiple things.
First, it creates a list
all_split by splitting the string
s using separator
sep. For example, when using it on string
sep='-', it would return
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'].
👉 Recommended Tutorial: Python String Split
Second, it combines the first
k elements using the separator string
sep between them by running
Recommended Tutorials: 👇
- Python String Join for
- Python Slicing for
- Python Split String at First Occurrence
Third, it puts the result into a list using the square bracket notation, i.e., we get a list with one string element
['a-b'] for our example.
👉 Recommended Tutorial: How to Create a Python List?
Fourth, you concatenate this list with the remaining
all_split list, ignoring the first k split results, that are already merged to ignore the first split, by using the slicing expression
In our example, we get
['c', 'd', 'e', 'f', 'g', 'h'] that concatenates to
['a-b', 'c', 'd', 'e', 'f', 'g', 'h'].
👉 Recommended Tutorials: List Concatenation in Python
Join Us Free!
Thanks for reading this short tutorial. To keep learning, feel free to join my 100% free email academy with cheat sheets and regular learning content!
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.