Как добавить несколько разделителей в split python
Перейти к содержимому

Как добавить несколько разделителей в split python

  • автор:

Learn How To Use Split Function In Python

Python programming language has various data types including strings. Even though strings are immutable in nature, we can still manipulate a string using functions like a split function. It breaks down larger strings into smaller strings using different parameters. In this article, we will learn about how we can use the split function in python. Following are the topics discussed in this article:

  • What Is A String?
  • Need For Split Function?
  • How To Use Split Function In Python?
  • Split Parameters
  • Separator
  • Max
  • Example

What Is A String?

Strings in python represent unicode character values. Python does not have a character data type, a single character is also considered as a string.

We use the single or double quotes to declare a string. To access a string, we use the indexes and square brackets. Since strings are mutable in nature, we cannot make any changes after declaring a string.

Although we cannot change a string after declaration, we can split a string in python.

Need For Split Function

Split function returns a list of strings after dividing the string based on the given separator. Following are the advantages of using a split function in python:

  • At some point we may have to break down a large string into smaller chunks or strings.
  • It is the opposite of concatenation, which adds two strings together.
  • The white spaces are considered as a separator if none is provided in the split function.
  • It becomes easier to analyze and deduct conclusions.
  • It helps to decode encrypted strings.

How To Use Split Function In Python?

Split function breaks down a larger string and gives a list with smaller chunks or strings. Below is an example to split a string in python.

Above is a simple example to show how split function can be used to break down the whole text into smaller strings. But split function has different parameters to optimize the execution.

Split Parameters

  1. Separator — It acts like a delimiter, the string is broken down according to the separator specified. It is optional as well, if there is no separator specified, the default separator will be the white space.
  2. Max — It is optional as well. It defines the number of splits that will take place. The default value is -1 which means no limits on the number of splits.


Below is an example to show the split function with a separator parameter:

In the above example, the separator is specified according to which the string is split into smaller strings.

Below is an example to show the split function with a max parameter:

The max parameter in the above example is set to 3, which means the output will have 4 elements in the list of strings.


Below are a few examples, where we can use the split function to split the string into smaller chunks or strings.

In this blog, we have learnt how split function can be used to break down large strings into smaller chunks or strings. String is an immutable data structure which means it cannot be changed once you have declared it. Although manipulation can be done using split function. Python programming language has different data types like list, dictionary, tuple, set etc.

Primitive data types and specialized data structures optimize your code and gives an edge to python over other programming languages. To master your skills enroll to Python online training program and kick-start your learning.

If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series which will explain the various other aspects of Python and Data Science.

Split string with multiple delimiters in Python [duplicate]

I found some answers online, but I have no experience with regular expressions, which I believe is what is needed here.

I have a string that needs to be split by either a ‘;’ or ‘, ‘ That is, it has to be either a semicolon or a comma followed by a space. Individual commas without trailing spaces should be left untouched

should be split into a list containing the following:

jww's user avatar

5 Answers 5

Luckily, Python has this built-in 🙂

Following your comment:

Błażej Michalik's user avatar

Do a str.replace(‘; ‘, ‘, ‘) and then a str.split(‘, ‘)

Here’s a safe way for any iterable of delimiters, using regular expressions:

re.escape allows to build the pattern automatically and have the delimiters escaped nicely.

Here’s this solution as a function for your copy-pasting pleasure:

If you’re going to split often using the same delimiters, compile your regular expression beforehand like described and use RegexObject.split .

If you’d like to leave the original delimiters in the string, you can change the regex to use a lookbehind assertion instead:

(replace ?<= with ?= to attach the delimiters to the righthand side, instead of left)

Python: Split a String on Multiple Delimiters

Python Split a String on Multiple Delimiters Cover Image

In this tutorial, you’ll learn how to use Python to split a string on multiple delimiters. You’ll learn how to do this with the built-in regular expressions library re as well as with the built-in string .split() method.

But why even learn how to split data? Splitting data can be an immensely useful skill to learn. Data comes in all shapes and it’s often not as clean as we would like to be. There will be many times when you want to split a string by multiple delimiters to make it more easy to work with.

Now lets get started!

Table of Contents

How do you split a string in Python?

Python has a built-in method you can apply to string, called .split() , which allows you to split a string by a certain delimiter.

The method looks like this:

In this method, the:

  • separator: argument accepts what character to split on. If no argument is provided, it uses any whitespace to split.
  • maxsplit: the number of splits to do, where the default value is -1 , meaning that all occurrences are split.

Let’s say you had a string that you wanted to split by commas – let’s learn how to do this:

We can see here that what’s returned is a list that contains all of the newly split values.

Split a Python String on Multiple Delimiters using Regular Expressions

The most intuitive way to split a string is to use the built-in regular expression library re . The library has a built in .split() method, similar to the example covered above. What’s unique about this method is that it allows you to use regular expressions to split our strings.

Let’s see what this method looks like:

Similar to the example above, the maxsplit= argument allows us to set how often a string should be split. If it’s set to any positive non-zero number, it’ll split only that number of times.

So, let’s repeat our earlier example with the re module:

Now, say you have a string with multiple delimiters. The re method makes it easy to split this string too!

Let’s take a look at another example:

What we’ve done here is passed in a raw string that re helps interpret. We pass in the pipe character | as an or statement.

We can simplify this even further by passing in a regular expressions collection. Let’s see how we can do this:

This returns the same thing as before, but it’s a bit cleaner to write and to read.

Split a Python String on Multiple Delimiters using String Split

You’re also able to avoid use of the re module altogether. The module can be a little intimidating, so if you’re more comfortable, you can accomplish this without the module as well.

In the example below, you’ll learn how to split a Python string with multiple delimiters by first replacing values. We’ll take our new string and replace all delimiters to be one consistent delimiter. Let’s take a look:

This method works fine when you have a small number of delimiters, but it quickly becomes messy when you have more than 2 or 3 delimiters that you would want to split your string by. It’s better to stick to the re module for more complex splits.

Create a Function to Split a Python String with Multiple Delimiters

Finally, let’s take a look at how to split a string using a function. For this function, we’ll use the re module. You’ll be able to pass in a list of delimiters and a string and have a split string returned.

Let’s get started!


In this post, you learned how to split a Python string by multiple delimiters. You learned how to do this using the built-in .split() method, as well as the built-in regular expression re ‘s .split() function.

To learn more about splitting Python strings, check out the .split() method’s documentation here. To learn more about splitting strings with re , check out the official documentation here.

How to Split String with Multiple Delimiters in Python

To split a string with a single delimiter in Python, use the string.split() method. The string split() is a built-in Python function that splits the string into a list.


In this example, we split the string from the comma, a delimiter in the string.

Python split multiple delimiters

To split a string with multiple delimiters in Python, use the re.split() method. The re.split() function splits the string by each occurrence of the pattern.

A delimiter is a series of characters that define the boundary between separate, independent regions in plain text or other data streams.


The re.split() function target string as per the regular expression pattern, and the matches are returned in a list.

Our example shows that the string contains various symbols like “,”, and “;”. Therefore, we have split the string from these two delimiters using the re.split() method.

String split using replace() method

Python string replace() is a built-in function that returns the copy of the string where all occurrences of the substring are replaced with another substring.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *