
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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']
- Related Articles
- How to split a string with a string delimiter in C#?
- How do we use a delimiter to split string in Python regular expression?
- How can we split a string by sentence as a delimiter in Java?
- How to split a string in Python
- Write a Python function to split the string based on delimiter and convert to series
- Java regex program to split a string with line endings as delimiter
- Python Program to Split a String by Custom Lengths
- How to split string on whitespace in Python?
- How can I use Python regex to split a string by multiple delimiters?
- How to split a string in Java?
- How to split a string in Golang?
- Python program to split and join a string?
- Split the left part of a string by a separator string in MySQL?
- How to split a string in Lua programming?
- How to use Java String.split() method to split a string by dot?
