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

  • Remove Urls from String in Python
    03 May

    Remove Urls from Text in Python

    Table of ContentsIntroductionWays to remove URLs from Text in PythonUsing the re.sub() function to remove URLs from Text in PythonUsing the re.findall() function to remove URLs from Text in PythonUsing the re.search() function to remove URLs from Text in PythonUsing the urllib.urlparse class to remove URLs from Text in PythonConclusion In this post, we will […]

  • 16 February

    Remove Backslash from String in Python

    Table of ContentsHow to remove backslash from string in python?Using replace() Function to Remove Backslash from String in PythonUsing the decode() Function to Remove Backslash from String in PythonUsing re Library Functions to Remove Backslash from String in PythonUsing strip() Function to Remove Backslash from String in PythonHow To Remove Backslash from Json String in […]

  • Remove quotation marks from String in Python
    13 February

    Remove Quotation Marks from String in Python

    Table of ContentsQuotes in PythonRemove Double Quotes from String in PythonUsing the replace() Function to Remove Double Quotes from String in PythonUsing the re.sub() Function to Remove Double Quotes from String in PythonUsing the for Loop to Remove Double Quotes from String in PythonUsing the join() Function to Remove Double Quotes from String in PythonUsing […]

  • Parentheses from String in Python
    08 February

    Remove Parentheses from String in Python

    Table of ContentsWays to Remove Parentheses from String in PythonUsing the replace() Function to Remove Parentheses from String in PythonUsing the re.Sub() Function to Remove Parentheses from String in PythonUsing the pandas.Str.Replace() Function to Remove Parentheses from String in PythonConclusion In this article, we will see how to remove parentheses from String in Python. Parentheses […]

  • Remove word from String in Python
    08 February

    Remove Word from String in Python

    Table of ContentsRemove Word from String in PythonHow to Remove Word from Sentence in PythonUsing the replace() functionUsing the re.sub() functionUsing the startswith() functionUsing the removeprefix() functionUsing the endswith() functionUsing the removesuffix() functionHow to Remove Duplicate Words from String in PythonUsing the set() functionUsing the set() and join() functionsUsing the join() and a user-defined functionUsing […]

  • 22 January

    Replace space with underscore in Python

    Table of ContentsWays to replace space with underscore in PythonUsing the for loopUsing the replace() functionUsing the re.sub() functionUsing the split() and join() functionConclusion Strings are an essential data type in programming. In Python, we can treat strings as an iterable of characters, and can perform a variety of functions and operations on them. Replacing […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.