Special Offer - Enroll Now and Get 2 Course at ₹25000/- Only Explore Now!

All Courses
How to use Split Function in Python

How to use Split Function in Python

January 14th, 2020

Split Function in Python

In python we have used different data types is that one of the types is String. The string is considered it is immutable. i.e you cannot able to manipulate the string. These kinds of split function will help to manipulate. This split function helps to breaks the string into substrings i.e array of small strings. Before learning about split first we need knowledge about String.


Normally string is a sequence of character values. It is also called a sequence of Unicode characters. Why its called Unicode means we can any values like characters as well as numbers in a string. It is also immutable in nature. The string will represent either a single quote or double quotes in python. Because here there is no character data type.



Need for split

The split function is used to break the string into a list of strings. It has some advantages also

  • It becomes easy to analyze the strings for deductions.
  • It is used to divide the larger string into smaller parts.
  • Used for encrypt the strings.


split(separator,max split)
separator – It is another string used for splitting the string. It is also considered as a delimiter for splitting the string. The default is space. i.e you are not passing any parameter it will split the string using space.
max split – It is a numeric value to indicates the number of strings we need after the split. i.e split will return the list of strings on that we fix the length of the list. If you given -1 means no limit for splitting. It is an optional parameter.


s=”This is python”

In this example, we use the default delimiter space to split the string into list of strings. Here we also not mention the max split also.


[‘This’, ‘is’, ‘python’]


s=”Red ball,blue ball,green ball”

In this example we use the delimiter string as ‘,’ to split the string into list of strings. Here instead of space we use ‘,’. Also we are not given maxsplit.


[‘Red ball’, ‘blue ball’, ‘green ball’]

Using Max split parameter


s=”Red ball,blue ball,green ball”

In this example, we use the max split parameter as 1 with a delimiter of ‘,’. Normally it will split the strings into a number of strings in the previous examples. But here we restrict the split into only 2 strings. i.e max split value is 1 means 0 and 1. So that split method divided the string into only two strings in a list. Normally it’s 3.


[‘Red ball’, ‘blue ball,green ball’]


print([s[i:i+3] for i in range(0,len(s),3)])

In this example we are split the strings without using split method. Here we are not using delimeter also to split. Just use the logic with slicing to split the string into list of strings it is also the way to split string.


[‘ant’, ‘bee’, ‘cat’, ‘dog’]

Related Blogs