Home >Backend Development >Python Tutorial >How to Split a String in Python Using a Specified Delimiter?
How to Perform String Splitting Using Delimiters in Python
String manipulation is a fundamental aspect of programming, and the ability to split strings into smaller segments based on specified delimiters is often necessary. This article addresses a specific question regarding string splitting in Python.
Question:
How can I split the following input string using a specified delimiter:
'MATCHES__STRING'
The desired output should be a list of strings:
['MATCHES', 'STRING']
Answer:
To achieve this string splitting in Python, we leverage the robust str.split method. Here's the code snippet to illustrate:
input_string = "MATCHES__STRING" delimiter = "__" output = input_string.split(delimiter) print(output)
Explanation:
The str.split method takes a delimiter as its argument and splits the string into a list of substrings, separating them based on the occurrence of the delimiter. In our case, we specify the delimiter as "__", indicating that we want to split the string whenever "__" appears.
The output of the code is a list of strings:
['MATCHES', 'STRING']
This meets the desired objective of splitting the string using the provided delimiter.
The above is the detailed content of How to Split a String in Python Using a Specified Delimiter?. For more information, please follow other related articles on the PHP Chinese website!