Split String With Multiple Delimiters In Python

Split String with multiple delimiters

In this post, we will see how to split String with multiple delimiters in Python.

Splitting a string in Python

When we talk about splitting a string, it means creating a collection of sub-strings out of a string. We can split a string by a character and create a list of sub-strings. In Python, we can split a string by single or multiple delimiter characters.

Ways to split a string in Python

We will now discuss how to split a string based on multiple delimiters in python.

Using the split() and replace() functions

We can use the split() function to split a string based on some delimiter in Python. First, we will replace the second delimiter with the first delimiter using the replace() function.

Then, we proceed to split the string using the first delimiter using the split() function that will return a list of sub-strings.

See the code below.

Output:

[‘split’, ‘ the’, ‘ string’, ‘ multiple’, ‘ delimiters’]

In the above example,

  • We replace the ; character with , using the replace() function.
  • Then we split the string based on the , character using the string() function.

Using the regular expressions

Regular expressions are clean, compact patterns that can match parts of the strings. To use regular expressions in Python, we use the re library.

We use the re.split() to split the string based on the occurrence of some pattern. To split a string using multiple delimiters, we specify multiple delimiters in the pattern.

For example,

Output:

[‘split’, ‘ the’, ‘ string’, ‘ multiple’, ‘ delimiters’]

We can also use this function in another way. First, we will create a pattern using the re.compile() function and then use it separately with the re.split() function.

For example,

Output:

[‘split’, ‘ the’, ‘ string’, ‘ multiple’, ‘ delimiters’]

This method is a good way to use regular expressions as it allows reusability of the pattern.

Related Posts

  • String to char Array in Python
    06 November

    Convert String to Char array in Python

    Table of ContentsConvert String To Char Array In Python Using For LoopConvert String To Char Array In Python Using The list() ConstructorConvert String To Char Array In Python Using The extend() MethodConvert String To Char Array In Python Using The += OperatorConvert String To Char Array In Python Using Packing And UnpackingConvert String To Char […]

  • Loop through String in Python
    28 October

    Loop through String in Python

    Table of ContentsUse the for loop to loop through String in PythonUsing the range() functionUsing the slicing [] operatorUsing the enumerate() functionUse the while loop to loop through String in PythonConclusion In this article, we will see how to loop through String in Python. Iteration or looping means executing a set of statements till a […]

  • Get variable name as String in Python
    01 October

    How to get variable name as String in Python

    Table of ContentsWhat are variable names in Python?How to get a variable name as a string in Python?In Python 2.xIn Python 3.x In Python, there are several ways to convert a string to a variable name, but the reverse process is not exactly utilized so often in the programming world. This article focuses on how […]

  • Python hex to bytes
    31 May

    Convert Hex to bytes in Python

    Table of ContentsUse codecs.decode() function to Convert Hexadecimal value to Byte in PythonUse bytes.fromhex() function to Convert Hexadecimal value to Byte in PythonUse the binascii Module to Convert Hexadecimal value to Byte in Python 💡 Outline You can use codecs’s decode() function to convert Hex to bytes in python. This method works for Python 2.x […]

  • Remove commas from String in Python
    23 May

    Remove comma from String in Python

    Table of ContentsUsing replace() function to remove comma from string in PythonUsing re or RegEx package function to remove comma from string in Python 💡 Outline You can remove comma from string in python by using string’sreplace() method. Syntax: [crayon-6194fc0e7ea7e714038949/] In this tutorial, we will take a sample string with a couple of commas in […]

  • Add character to String in Python
    21 May

    Add character to String in Python

    Table of ContentsUsing the + operatorUsing the join() functionUsing the % operatorUsing the format() functionUsing the f-strings Strings are considered as a sequence of characters in programming. In Python, string objects are considered immutable. What this means is that string objects cannot be changed once created, and a new string is created with a different […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.