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. 

Why the Python Split Function is Essential for Developers

  • 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

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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.

Practical Examples of the Split Function

  • 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

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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

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

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Python String split() Method

The syntax of the string split() method is as follows:

str.split(separator, maxsplit)

Example 1: Demonstrate How the split() Function Works

text = 'digital marketing'

# Splits at space

print(text.split())

word = 'digital, marketing'

# Splits at ','

print(word.split(','))

word = 'digital:marketing'

# Splitting at ':'

print(word.split(':'))

word = 'CutBatSitFatOr'

# Splitting at t

print(word.split('t'))

Output

['digital', 'marketing']

['digital', ' marketing']

['digital', 'marketing']

['Cu', 'Ba', 'Si', 'Fa', 'Or']

Example 2: Demonstrate How the split() Function Works on Specifying Maxsplit

word = 'digital, marketing, simplilearn, courses'

# maxsplit: 0

print(word.split(', ', 0)) 

# maxsplit: 4

print(word.split(', ', 4)) 

# maxsplit: 1

print(word.split(', ', 1))

Output

['digital, marketing, simplilearn, courses']

['digital', 'marketing', 'simplilearn', 'courses']

['digital', 'marketing, simplilearn, courses']

Split() Parameters

The split() method has the following two parameters maximum:

separator

  • It is optional.
  • A delimiter at which the split occurs.
  • If absent, the string splits at whitespaces.

maxsplit

  • It is optional.
  • It specifies the maximum number of splits. 
  • If absent, there is no limit on the number of splits. 

Split() Return Value

The return value of the split() method is always a list of strings obtained after breaking the given string by the specified separator.

Example 1: How Does split() Work in Python?

text= 'Love your parents'

# splits at space

print(text.split())

grocery = 'Vegetables, Milk, Bread'

# splits at ','

print(grocery.split(', '))

# Splits at ':'

print(grocery.split(':'))

Output

['Love', 'your', 'parents']

['Vegetables', 'Milk', 'Bread']

['Vegetables, Milk, Bread']

Example 2: How Does split() Work When maxsplit is Specified?

Whenever maxsplit is specified, the list has a maximum of maxsplit+1 items.

grocery = 'Milk, Vegetables, Bread, Butter'

# maxsplit: 2

print(grocery.split(', ', 2))

# maxsplit: 1

print(grocery.split(', ', 1))

# maxsplit: 5

print(grocery.split(', ', 5))

# maxsplit: 0

print(grocery.split(', ', 0))

Output

['Milk', 'Vegetables', 'Bread, Butter']

['Milk', 'Vegetables, Bread, Butter']

['Milk', 'Vegetables', 'Bread', 'Butter']

['Milk, Vegetables, Bread, Butter']

How to Use the Split() Method Without Parameters?

String splitting can also be done using the split() method without passing any parameters.

myString = "Python is an easy language"

print(myString.split())

Output

['Python', 'is', 'an', 'easy', 'language']

The above code has a string called myString with five different characters that make up the string: "Python is an easy language".

On using the split() method on myString and printing it, each character becomes a separate item in a list: ['Python', 'is', 'an', 'easy', 'language'].

Reason: 

The split() method separates each word because, by default, whitespaces are an indication of the point of splitting.

How to Use the Split() Method With Parameters?

The following example shows splitting using the split() method's parameters:

myString = "Dear Friends, if you love reading, read on"

print(myString.split(", "))

Output

['Dear Friends', "if you love reading", "read on"]

The above example has a comma (,) as the separator: myString.split(", ").

So, rather than splitting the characters after every whitespace, the characters only split when a comma appears. Therefore, the characters appearing before a comma are grouped together.

The following example shows the use of the second parameter – maxsplit.

myString = "Dear Friends, if you love reading, read on"

print(myString.split(", ", 0))

Output

["Dear Friends, if you love reading, read on"]

When maxsplit value is specified as 0, it implies 1. Thus, the characters are returned as one item in a list. 

On changing the number, the results change as follows: 

myString = "Dear Friends, if you love reading, read on"

print(myString.split(", ", 1))

Output

['Dear Friends', "if you love reading, read on"]

On changing the number to 1, the characters get separated into two items in the list: ''Dear Friends'' and "if you love reading, read on".

Skipping the maxsplit value sets it to -1(by default). The negative value enables the split() method to split every character continuously into separate items. When a separator is specified,  the splitting is done with respect to that value. Else whitespaces are used.

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.

If you are looking to further enhance your skills, we highly recommend you to check Simplilearn's Post Graduate Program in Full Stack Web Development. This course can help you hone the right skills and make you job-ready.

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!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 16 Dec, 2024

6 Months$ 8,000
Full Stack Java Developer Masters Program

Cohort Starts: 18 Dec, 2024

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449
Automation Test Engineer Masters Program8 months$ 1,499