Blog

Blog

Learn How To Use Split Function In Python

Learn How To Use Split Function In Python

What is a Split Function 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 split() method in Python is a method of the str class that is used to split a string into a list of substrings. It is called on a string object and takes an optional separator string as an argument. If no separator is provided, the method splits the string on any whitespace characters.

Youtube banner Logo

Here is an example of using the split() method:

# Split the string on commas
string = "apple,banana,cherry"
list = string.split(',')
print(list)  # Output: ['apple', 'banana', 'cherry']

# Split the string on whitespace
string = "apple banana cherry"
list = string.split()
print(list)  # Output: ['apple', 'banana', 'cherry']

The split() method returns a list of substrings that were obtained by splitting the input string on the specified separator. It can be very useful for parsing strings that contain multiple values separated by a common delimiter, such as a CSV (comma-separated values) file.

How To Use Split Function in Python?

The split() method in Python is used to split a string into a list of substrings. It is defined in the string class and can be called on any string object.

Here is the basic syntax for using the split() method:

string.split(separator, maxsplit)

separator is a string that separates the substrings in the input string. If it is not provided, any whitespace string is a separator and maxsplit specifies how many splits to do. If it is not provided, then the string is split into as many substrings as possible.

Here is an example of using the split() method:

# Split the string on commas
string = "apple,banana,cherry"
list = string.split(',')
print(list)  # Output: ['apple', 'banana', 'cherry']

# Split the string on whitespace
string = "apple banana cherry"
list = string.split()
print(list)  # Output: ['apple', 'banana', 'cherry']

# Split the string into at most 3 substrings
string = "apple,banana,cherry,date,elderberry"
list = string.split(',', 3)
print(list)  # Output: ['apple', 'banana', 'cherry', 'date,elderberry']

What is 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

The split() function in Python is often used when working with large datasets or logs that are stored in string form. For example, consider the following string that contains a list of fruits:

"apple,banana,cherry,date,elderberry"

If you want to work with this list of fruits in your Python code, it would be much easier to store them in a list rather than a string. You can use the split() function to split the string on the ',' characters and convert it into a list of strings:

fruits = "apple,banana,cherry,date,elderberry"
fruits_list = fruits.split(',')
print(fruits_list)  # Output: ['apple', 'banana', 'cherry', 'date', 'elderberry']

Now that the fruits are stored in a list, you can easily access and manipulate them in your code. For example, you could loop through the list and print each fruit, or you could use the len() function to get the number of fruits in the list.

In general, the split() function is very useful for parsing strings that contain multiple values or for working with large datasets that are stored in string form. It allows you to easily convert strings into lists, which can then be manipulated and analyzed in your Python code.

The working of the 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.

The split() function is a method in Python that is used to split a string into a list of substrings. The syntax for using the split() function is:

string.split(separator, maxsplit)

Where:

  • separator is a string that is used to split the string. If this parameter is not provided, the split() function will use any whitespace characters as the separator.
  • maxsplit is an optional parameter that specifies the maximum number of splits to be done. If this parameter is not provided, the string will be split into all possible parts.

Here’s an example of using the split() function:

>>> s = 'Hello, World!'
>>> s.split(',')
['Hello', ' World!']
>>> s.split()
['Hello,', 'World!']

In the first example, the string is split at the comma. In the second example, the string is split at any whitespace character (in this case, the space character).

Split in Python: An Overview of Split() Function

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

The working of the 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 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.

A Split() Function Can Be Used in Several Ways. They Are:

There are several ways in which the split() function can be used:

  1. By providing a separator: In this case, the string will be split at every occurrence of the separator. For example:
>>> s = 'Hello, World!'
>>> s.split(',')
['Hello', ' World!']
  1. Without providing a separator: If no separator is provided, the split() function will use any whitespace character as the separator. For example:
>>> s = 'Hello, World!'
>>> s.split()
['Hello,', 'World!']
  1. By specifying the maximum number of splits: The maxsplit parameter can be used to specify the maximum number of splits to be done. For example:
>>> s = 'Hello, World!'
>>> s.split(',', 1)
['Hello', ' World!']

In this case, only one split is done at the comma, resulting in a list with two elements.

  1. By using the rsplit() function: The rsplit() function is similar to the split() function, but it works in the reverse direction, starting from the end of the string. For example:
>>> s = 'Hello, World!'
>>> s.rsplit(',', 1)
['Hello', ' World!']

This is similar to the previous example, but the split is done from the end of the string, rather than from the beginning.

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 Datavalley’

#using a split() function with space as a delimiter to split the given string into smaller strings

print(string_to_be_split.split(” “))

The output of the above program is shown below:

    [ 'We, 'love', 'Datavalley' ]

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 = ‘Datavalley’

#using a split() function with the first occurrence of a given character in the string as a delimiter to split the given string into smaller strings

print(string_to_be_split.split(“i”))

The output of the above program is shown below:

       ['D', 'ata', 'valley']

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 below:

['This is the first line in the file', 'Welcome', 'to', 'Datavalley']

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 Datavalley’

#using split() function with space as a delimiter to split the given string into smaller strings

print(string_to_be_split.split(“\n”))

The output of the above program is shown below:

['We', 'love', 'Datavalley']

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 Datavalley’

#using split() function with space as a delimiter to split the given string into smaller strings

print(string_to_be_split.split(“\t”))

The output of the above program is shown below:

['We', 'love', 'Datavalley']

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,  Datacademy’

#using split() function with space as a 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:

['We', 'love', 'Datacademy']

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 Datacademy’

#using re.split() function with a 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 below:

['We', 'love', 'Datacademy']

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:  Datacademy’

#using a split() function with: as a delimiter to split the given string into smaller strings

print(string_to_be_split.split(“:”))

The output of the above program is shown below:

['We', 'love', 'Datacademy']

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#  Datacademy’

#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 below:

['We, 'love', 'Datacademy']

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, Datacademy’

#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 below:

['We', 'love', 'Datacademy']

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 = ‘Datacademy’

#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 below:

['d', 'a', 't', 'a', 'c', 'a', 'd', 'e', 'm', 'y']

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, Datacademy’

#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:

['Welcome, ', ', Datacademy']

Python String split() Method

The split() method is a built-in method in Python that allows you to split a string into a list. The method takes in an optional delimiter, which is a character that separates the different parts of the string. If no delimiter is provided, the method will split the string at every whitespace character.

Here is an example of how you can use the split() method:

string = "Hello, how are you?"
parts = string.split(",")
print(parts)

Output:

['Hello', 'how are you?']

In this example, the split() method splits the string at the comma character, resulting in a list of two elements. The first element is the part of the string before the comma, and the second element is the part of the string after the comma.

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, datacademy, courses’

# maxsplit: 0

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

# maxsplit: 4

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

# maxsplit: 1

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

Output

[‘digital, marketing, datacademy, courses’]

[‘digital’, ‘marketing’, ‘datacademy’, ‘courses’]

[‘digital’, ‘marketing, datacademy, courses’]

split() Parameters

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

separatorIt is optional. A delimiter at which the split occurs. If absent, the string splits at whitespaces.
maxsplitIt 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.

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
Click outside to hide the comparison bar
Compare

Subscribe to Newsletter

Stay ahead of the rapidly evolving world of technology with our news letters. Subscribe now!