Categories
Python Answers

How to split a string by a delimiter in Python?

Spread the love

Sometimes, we want to split a string by a delimiter in Python.

In this article, we’ll look at how to split a string by a delimiter in Python.

How to split a string by a delimiter in Python?

To split a string by a delimiter in Python, we can use the string’s split method.

For instance, we write:

a = "MATCHES__STRING".split("__")
print(a)

We call split on the string with the separator we want to split the string with as the argument.

Then we assign the returned list to a.

Therefore, a is ['MATCHES', 'STRING'].

Conclusion

To split a string by a delimiter in Python, we can use the string’s split method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *