The string manipulation function in Python used to break down a bigger string into several smaller strings is called the split() function in Python. The split() function returns the strings as a list.
A Split() Function Can Be Used in Several Ways. They Are:
- Splitting the string based on the delimiter space
- Splitting the string based on the first occurrence of a character
- Splitting the given file into a list
- Splitting the string based on the delimiter new line character
- Splitting the string based on the delimiter tab
- Splitting the string based on the delimiter comma
- Splitting the string based on multiple delimiters
- Splitting the string into a list
- Splitting the string based on the delimiter hash
- Splitting the string by passing maxsplit parameter
- Splitting the string into a character array
- Splitting the string based on one of the substrings from the given string as the delimiter
Splitting the String Based on the Delimiter Space:
The given string or line is separated using the split() function with whitespace as the delimiter.
Example 1:
Python program to demonstrate split() function in Python with space as delimiter:
#creating a string variable to store the string to be split
string_to_be_split = 'We love Simplilearn'
#using split() function with space as delimiter to split the given string into smaller strings
print(string_to_be_split.split(" "))
The output of the above program is shown in the snapshot below:
Want a Top Software Development Job? Start Here!
Full Stack Developer - MERN StackExplore Program
Splitting the String Based on the First Occurrence of a Character:
The given string or line is separated using the split() function with the first occurrence of the character from the string specified as the delimiter.
Example 2:
Python program to demonstrate split() function in Python with the first occurrence of a given character in the string as delimiter:
string_to_be_split = 'Simplilearn'
#using split() function with the first occurrence of a given character in the string as delimiter to split the given string into smaller strings
print(string_to_be_split.split("i"))
The output of the above program is shown in the snapshot below:
Splitting the Given File Into a List:
The data in the file is split into several lines and each line is returned as an element in the list by making use of a split function called the splitlines() function in Python.
Example 3:
Python program to demonstrate splitlines() function in Python to split the data in the given file into a list:
#opening a file in read mode using open() function
fileopen = open("C:/Users/admin/Desktop/images/example.txt", "r")
#reading the contents of the file using read() function
fileread = fileopen.read()
#using splitlines() function to display the contents of the file as a list
print(fileread.splitlines())
fileopen.close()
The output of the above program is shown in the snapshot below:
Want a Top Software Development Job? Start Here!
Full Stack Developer - MERN StackExplore Program
Splitting the String Based on the Delimiter New Line Character:
The given string or line is separated using a split() function with a new line character as the delimiter.
Example 4:
Python program to demonstrate split() function in Python with new line character as delimiter:
string_to_be_split = 'We\n love\n Simplilearn'
#using split() function with space as delimiter to split the given string into smaller strings
print(string_to_be_split.split("\n"))
The output of the above program is shown in the snapshot below:
Splitting the String Based on the Delimiter Tab:
The given string or line is separated using the split() function with a tab as the delimiter.
Example 5:
Python program to demonstrate split() function in Python with tab as the delimiter:
string_to_be_split = 'We\t love\t Simplilearn'
#using split() function with space as delimiter to split the given string into smaller strings
print(string_to_be_split.split("\t"))
The output of the above program is shown in the snapshot below:
Splitting the String Based on the Delimiter Comma:
The given string or line is separated using the split() function with a comma as the delimiter.
Example 6:
Python program to demonstrate split() function in Python with delimiter comma:
string_to_be_split = 'We, love, Simplilearn'
#using split() function with space as delimiter to split the given string into smaller strings
string_after_split = string_to_be_split.split(",")
print(string_after_split)
The output of the above program is shown in the snapshot below:
Splitting the String Based on Multiple Delimiters:
Multiple delimiters can be specified as a parameter to the split() function by separating each delimiter with a |. The given string or line with multiple delimiters is separated using a split function called re.split() function.
Example 7:
Python program to demonstrate re.split() function in Python to split a given string or a line with multiple delimiters:
#importing the module re
import re
string_to_be_split = 'We, love\n Simplilearn'
#using re.split() function with comma and new line character as delimiters to split the given string into smaller strings
print(re.split(",|\n", string_to_be_split))
The output of the above program is shown in the snapshot below:
Splitting the String Into a List:
The given string or line can be split into a list using the split() function with any delimiters.
Example 8:
Python program to demonstrate split() function in Python to split a given string or a line with any delimiter:
string_to_be_split = 'We: love: Simplilearn'
#using split() function with : as delimiter to split the given string into smaller strings
print(string_to_be_split.split(":"))
The output of the above program is shown in the snapshot below:
Splitting the String Based on the Delimiter Hash:
The given string or line is separated using the split() function with a hash as the delimiter.
Example 9:
Python program to demonstrate split() function in Python to split a given string or a line with delimiter as hash:
string_to_be_split = 'We# love# Simplilearn'
#using split() function with # as delimiter to split the given string into smaller strings
print(string_to_be_split.split("#"))
The output of the above program is shown in the snapshot below:
Splitting the String by Passing the Maxsplit Parameter.
The maximum number of splits that a split() function can perform on a given string or line can be specified using the maxsplit parameter and passed as an argument to the split() function.
Example 10:
Python program to demonstrate split() function in Python with maxsplit parameter:
string_to_be_split = 'Welcome, to, Simplilearn'
#using split() function with maxsplit parameter to split the given string into smaller strings
print(string_to_be_split.split(" ", 2))
The output of the above program is shown in the snapshot below:
Splitting the String Into a Character Array:
The given string or line can be split into a list consisting of each character as the elements of the list using the split function called list() function.
Example 11:
Python program to demonstrate list() function in Python to split a given string or a line into several characters each one being the element in a list:
string_to_be_split = 'Simplilearn'
#using list() function to split the given string into a list
print(list(string_to_be_split))
The output of the above program is shown in the snapshot below:
Splitting the String Based on One of the Substrings From the Given String as the Delimiter:
The split() function can be used to split a given string or a line by specifying one of the substrings of the given string as the delimiter. The string before and after the substring specified as a delimiter is returned as the output.
Example 12:
Python program to demonstrate split() function in Python to split a given string or a line into several characters with one of the substrings of the given string being the delimiter:
string_to_be_split = 'Welcome, to, Simplilearn'
#using split() function with one of the substrings of the given string as the delimiter to split the given string into smaller strings
print(string_to_be_split.split("to"))
The output of the above program is shown in the snapshot below:
Want a Top Software Development Job? Start Here!
Full Stack Developer - MERN StackExplore Program