How to split a string with multiple delimiters in Python

In this Python tutorial, we will learn how to split a string by delimiter in Python and also we will discuss, how to split a string with multiple delimiters in Python.

When working with strings in Python, it is often necessary to split them into smaller pieces based on certain delimiters. In some cases, the delimiter may be a single character, such as a comma or a period. However, there may be instances where a string needs to be split based on multiple delimiters, such as spaces, newlines, and tabs. In this article, we will discuss how to split a string with multiple delimiters in Python, using various techniques.

Splitting a String by Delimiter in Python

In Python, we can split a string using the built-in split() method. This method returns a list of substrings that were separated by the specified delimiter. Here is the syntax for the split() method:

string.split(delimiter, maxsplit)
  • string is the string to split.
  • delimiter is the character or set of characters that separates the substrings. This is an optional argument, and if it is not provided, the method will use whitespace as the delimiter.
  • maxsplit is the maximum number of splits to perform. This is an optional argument, and if it is not provided, the method will split the string into as many substrings as possible.
READ:  Write String to a .csv File in Python

How to split a string by delimiter in Python

Let us see a few examples of how to split a string by delimiter in Python.

Example-1: Split a string by delimiter in Python using the vertical bar (|) delimiter

Let us see an example of how to split a string by delimiter in Python using the vertical bar (|) delimiter.

colors = "red|green|blue"
color_list = colors.split("|")
print(color_list)

Output:

['red', 'green', 'blue']

Example-2: Split a string in Python using the newline (\n) delimiter

Let us see now how to split a string in Python using the newline (\n) delimiter.

text = "Line 1\nLine 2\nLine 3"
lines = text.split("\n")
print(lines)

Output:

['Line 1', 'Line 2', 'Line 3']

Example-3: Split a string in Python using the semicolon (;) delimiter

Let us see another example of how to split a string in python using the semicolon (;) delimiter.

data = "John;Smith;35"
fields = data.split(";")
print(fields)

Output:

['John', 'Smith', '35']

In these examples, we used different delimiters to split the strings into substrings using the split() method in Python.

How to split a string with multiple delimiters in Python

Let us see a few examples of how to split a string with multiple delimiters in Python.

When working with strings in Python, it is often necessary to split them into smaller pieces based on certain delimiters. In some cases, the delimiter may be a single character, such as a comma or a period.

However, there may be instances where a string needs to be split based on multiple delimiters, such as spaces, newlines, and tabs. In this article, we will discuss how to split a string with multiple delimiters in Python, using various techniques.

  • Using the split() Method

The split() method is a built-in function in Python that splits a string into a list of substrings based on a specified delimiter. By default, the delimiter used by the split() method is a space character. However, the method can also accept multiple delimiters as arguments. The following code snippet demonstrates how to split a string using multiple delimiters:

string = "This is\na\ttest"
delimiters = " \n\t"
result = re.split("|".join(delimiters), string)
print(result)

In this example, the string “This is\na\ttest” is split into a list of substrings using the delimiters ” “, “\n”, and “\t”. The join() method is used to concatenate the delimiters into a regular expression pattern that is passed as an argument to the split() method. The resulting list contains the substrings “This”, “is”, “a”, and “test”.

  • Using the re.split() Function
READ:  How to Sum Elements in List in Python using For Loop

The re.split() function is a more powerful alternative to the split() method. It allows for more complex regular expression patterns to be used as delimiters. The following code snippet demonstrates how to split a string using multiple delimiters with the re.split() function:

import re

string = "This is\na\ttest"
delimiters = r"[\n\t ]+"
result = re.split(delimiters, string)
print(result)

In this example, the regular expression pattern “[\n\t ]+” matches one or more occurrences of the characters “\n”, “\t”, and ” “. The resulting list contains the same substrings as the previous example.

  • Using the splitlines() Method

The splitlines() method is a built-in function in Python that splits a string into a list of lines based on the newline character “\n”. However, it can also accept additional delimiters as arguments. The following code snippet demonstrates how to split a string using multiple delimiters with the splitlines() method:

string = "This is\na\ttest"
delimiters = " \t"
lines = string.splitlines()
result = [item for line in lines for item in line.split(delimiters)]
print(result)
how to split a string with multiple delimiters in Python
how to split a string with multiple delimiters in Python

In this example, the splitlines() method is first used to split the string into a list of lines. Then, the split() method is called on each line with the delimiters ” ” and “\t” as arguments. The resulting list contains the same substrings as the previous examples.

Examples of splitting a string with multiple delimiters in Python

Let us see a few examples of how to split a string with multiple delimiters in Python.

Example-1: Using Regular Expressions to Split a String with Multiple Delimiters

Regular expressions can be used to split a string with multiple delimiters in Python. The following example demonstrates how to split a string using a regular expression that matches the characters ” “, “\n”, and “\t”:

import re

string = "This is\na\ttest"
delimiters = r"[ \n\t]+"
result = re.split(delimiters, string)
print(result)

In this example, the regular expression pattern “[ \n\t]+” matches one or more occurrences of the characters ” “, “\n”, and “\t”. The resulting list contains the same substrings as the previous examples.

READ:  How to Capitalize First and Last Letters of Each Word in a String in Python [8 Ways]

Example-2: Splitting a String with Multiple Delimiters into a Dictionary

In some cases, it may be useful to split a string with multiple delimiters into a dictionary. The following example demonstrates how to split a string into a dictionary using the delimiters “:”, ” “, and “,”:

string = "name:John,age:30,gender:Male"
delimiters = ":, "
result = dict(zip(string.split(delimiters)[::2], string.split(delimiters)[1::2]))
print(result)

In this example, the split() method is used to split the string into a list of substrings using the delimiters “:”, ” “, and “,”. The resulting list is then converted into a dictionary using the zip() and dict() functions.

Example-3: Splitting a String with Multiple Delimiters and Ignoring Empty Strings

In some cases, splitting a string with multiple delimiters can result in empty strings. The following example demonstrates how to split a string while ignoring empty strings:

string = "This is\na\ttest\n"
delimiters = " \n\t"
result = [item for item in re.split("|".join(delimiters), string) if item]
print(result)

In this example, the join() method is used to concatenate the delimiters into a regular expression pattern that is passed as an argument to the split() method. The resulting list contains the substrings “This”, “is”, “a”, and “test”, and the empty string at the end of the original string is ignored.

Conclusion

In this Python tutorial explains, how to split a string with multiple delimiters in Python. We can use the split() method, re.split() function, and splitlines() methods to split a string with multiple delimiters in Python.

You may like the following python tutorials: