How To Split A String With Multiple Delimiters In Python

Split a string with multiple delimiters in python

There are some ways that can split a string with multiple delimiters in Python. Let’s follow this tutorial to learn about it with the explanation and examples below.

Split A String in Python

Splitting a string means creating a list of substrings based on the separator and the current string. In Python, you can split a String by one or more separators.

Look at the example below to know more about it.

# Create a String named 'str'
str = 'Hello, I am crvt4722 from Learning to Share IT.'

# Split this String by the white space
arr = str.split()

print(arr)

Output:

['Hello,', 'I', 'am', 'crvt4722', 'from', 'Learning', 'to', 'Share', 'IT']

After learning more about splitting a string in Python, you will learn the next title to know how to split a string with multiple delimiters in Python.

Split A String With Multiple Delimiters In Python

Using the split() and replace() method

We can use the split() and replace() method to split a String with multiple Delimiters in Python. First, we replace all the separators you want to split into one separator by the replace() method. Then, you use the split() method to split a String by this separator.

This is the syntax of the replace() method.

Syntax:

string.replace(oldvalue, newvalue, count)

Parameters:

  • string: The String you want to change.
  • oldvalue: The substring in the current String that you want to replace.
  • newvalue: The String replaces the old value.
  • count: Optional. The amount of the old value you want to replace.

Return value:

A new String.

This is the syntax of the split() method.

Syntax:

string.split(separator, maxsplit)

Parameters:

  • string: The String you want to split.
  • separator: Split a String with this separator.
  • maxsplit: The number of splits.

Return value:

A list of String.

Look at the example below to know how to do this solution.

# Create a String named 'str'
str = 'Hello, I am crvt4722 + from- Learning * to Share, IT'

# Replace all the separators you want to split to ','/.
new_str= str.replace('+',',').replace('-',',').replace('*',',')

# Then, split the new String by ','
arr = new_str.split(',')

print(arr)

Output:

['Hello', ' I am crvt4722 ', ' from', ' Learning ', ' to Share', ' IT']

Using the split() method in re library with ‘|’

Using the split() method in the re library to solve this problem.

Syntax:

re.split(‘demimiter1 | delimiter2 |…..’,str)

Parameters:

The first parameters are the delimiters separated by ‘|’.

  • str: The String you want to split.

Return value:

A list of String.

Look at the example below to learn more about this method.

import re

# Create a String named 'str'
str = "Hello, I am crvt4722 + from- Learning * to Share, IT"

separator =',|/|-|\*|\+ '

# Split this String by this separator
arr = re.split(separator, str)

print(arr)

Output:

['Hello', ' I am crvt4722 ', 'from', ' Learning ', ' to Share', ' IT']

Using the split() method in re library with ‘[ ]’

The re library in Python has a split() method we can use to solve this problem. 

Syntax:

re.split(r'[delimiter1,delimiter2,…………]\s*’,str)

Parameters:

The first parameters are the delimiters separated by ‘[ ]’.

  • str: The String you want to split.

Return:

A list of String.

Look at the example below to learn more about this method.

import re

# Create a String named 'str'
str = "Hello, I am crvt4722 + from- Learning * to Share, IT"

# Split this String with multiple delimiters
arr = re.split(r'[+\-,*]\s*', str)

print(arr)

Output:

['Hello', 'I am crvt4722 ', 'from', 'Learning ', 'to Share', 'IT']

Summary

You can use the split() method combined with the replace() method or split() method in the ‘re’ library to split a String with multiple delimiters in Python. Choose the solution that is the most suitable for you. We hope this tutorial is helpful to you. Have an exciting learning experience. Thanks!

Maybe you are interested:

Leave a Reply

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