How to split string by a delimiter string in Python?


A string is a collection of characters that can represent a single word or a complete phrase. Unlike Java there is no need to declare Python strings explicitly we can directly assign string value to a literal.

A string is an object of the String class, which contains multiple built in functions and methods to manipulate and access strings.

In this article, we are going to find out how to split a string by a delimiter string in Python.

Using the split() method

One way split a string using a delimiter using the string class inbuilt method named split(). This method accepts a string value as a parameter and returns the updated delimiter string as output.

This also have an optional parameter representing the delimiter. If nothing is mentioned as a delimiter, then by default whitespace is considered as the delimiter. There is also another optional parameter maxsplit, which tells us how many splits the user wants.

Example 1

In the example given below, we are taking a string input that is separated by – and we are splitting using the split() method.

str1 = "Welcome to Tutorialspoint" print("The given input string is") print(str1) print("The string after split ") print(str1.split())

Output

The output of the above given program is,

The given input string is
Welcome to Tutorialspoint
The string after split
['Welcome', 'to', 'Tutorialspoint']

Example 2

In the following we are splitting a string by the delimiter #.

str1 = "Hello#how#are#you" print("The given input string is") print(str1) print("The string after split ") print(str1.split("#"))

Output

The given input string is
Hello#how#are#you The string after split ['Hello', 'how', 'are', 'you']

Using Regular Expressions

We can also split a string in python using regular expressions. To do so, you need to use the function split() of re library. It has 2 parameters, the delimiter and the input string. It returns the updated string as output.

Example 1

In the example given below, we are taking a string input that is separated by – and we are splitting using the split() method and we are taking maxsplit as 1.

str1 = "Welcome to Tutorialspoint" print("The given input string is") print(str1) print("The string after split ") print(str1.split(' ',1))

Output

The output of the above given example is,

The given input string is
Welcome to Tutorialspoint
The string after split
['Welcome', 'to Tutorialspoint']

Example 2

In the example given below, we are taking a string as input and we are splitting it with a delimiter “ ” using the re.split() method.

import re str1 = "Welcome to Tutorialspoint" print("The given input string is") print(str1) print("The string after split ") print(re.split(' ',str1))

Output

The output of the above given program is,

The given input string is
Welcome to Tutorialspoint
The string after splitted
['Welcome', 'to', 'Tutorialspoint']

Example 3

In the example given below, we are taking a string as input and we are splitting it with a delimiter “ ” using the re.split() method and maxsplit as 1.

import re str1 = "Welcome to Tutorialspoint" print("The given input string is") print(str1) print("The string after split ") print(re.split(' ',str1,1))

Output

The output of the above given example is,

The given input string is
Welcome to Tutorialspoint
The string after split
['Welcome', 'to Tutorialspoint']

Updated on: 19-Oct-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements