Home >Backend Development >Python Tutorial >How to Split a String by a Delimiter in Python?
Splitting a String by a Delimiter in Python
Often, you'll encounter situations where strings need to be split into smaller segments based on a specific delimiter. This guide provides a concise solution to this common programming challenge.
Problem Statement
Given an input string, for instance:
'MATCHES__STRING'
Split the string wherever the delimiter "__" appears, resulting in a list of two substrings:
['MATCHES', 'STRING']
Solution
To achieve this, utilize Python's str.split method:
string.split(delimiter)
For the given example, the code would be:
"MATCHES__STRING".split("__")
This code will split the string at each occurrence of the "__" delimiter, yielding the desired output:
['MATCHES', 'STRING']
The above is the detailed content of How to Split a String by a Delimiter in Python?. For more information, please follow other related articles on the PHP Chinese website!