Split In Python

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. 

The Necessity to Use the Split() Function in Python:

  • Whenever there is a need to break bigger strings or a line into several small strings, you need to use the split() function in Python.
  • The split() function still works if the separator is not specified by considering white spaces, as the separator to separate the given string or given line.

The syntax to define a split() function in Python is as follows:

split(separator, max)

where,

  • separator represents the delimiter based on which the given string or line is separated
  • max represents the number of times a given string or a line can be split up. The default value of max is -1. In case the max parameter is not specified, the split() function splits the given string or the line whenever a separator is encountered

Working of Split() Function Is as Follows:

  • Manipulation of strings is necessary for all of the programs dealing with strings. In such cases, you need to make use of a function called split() function in Python.
  • The split() function works by scanning the given string or line based on the separator passed as the parameter to the split() function.
  • In case the separator is not passed as a parameter to the split() function, the white spaces in the given string or line are considered as the separator by the split() function.
  • The number of times a given string or line must be broken up can also be specified using the split() function. In case if you do not specify this value, the entire string or line is scanned and separated based on the delimiter.
  • The split() function returns the substrings as the elements of a list.

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

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:

SplitInPythonEx_1

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:

SplitInPythonEx_2

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:

SplitInPythonEx_3

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:

SplitInPythonEx_4.

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:

SplitInPythonEx_5

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:

SplitInPythonEx_6.

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:

SplitInPythonEx_7

FREE Data Science With Python Course

Start Learning Data Science with Python for FREEStart Learning
FREE Data Science With Python Course

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:

SplitInPythonEx_8

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:

SplitInPythonEx_9

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:

SplitInPythonEx_10

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:

SplitInPythonEx_11

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:

SplitInPythonEx_12

Looking forward to making a move to the programming field? Take up the Python Training Course and begin your career as a professional Python programmer

Conclusion

In this article, you have learned the concept of ‘Split in Python’ using the split() function in Python. Simplilearn offers a Python Certification Course designed to help you learn everything in Python to kick start your career, and this is a very flexible way to acquire skills in Python.

Have any questions for us? Don’t hesitate to share them with us in the comments section of this page. Our subject matter experts will review them and get back to you on them in no time!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.