Home >Backend Development >Python Tutorial >How to Split a String by a Delimiter in Python?

How to Split a String by a Delimiter in Python?

DDD
DDDOriginal
2024-12-08 05:44:101069browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn