Python String Split Method

In python, the string split() method is useful to split the string into a list that contains substrings, and those are separated by the specified separator.

 

Following is the pictorial representation of string split() method functionality in python.

 

Python string split method example result

 

If you observe the above diagram, we are splitting the string "Suresh-Rohini-Trishika" with "-" separator using the python split method. Once splitting is done, the split method will return a list of words in the string.

String Split Method Syntax

Following is the syntax of defining a string split method in python.

 

string.split(separator, maxsplit)

If you observe the above syntax, the split() method is accepting two parameters (separator, maxsplit) and those are optional.

 

  • separator - It’s a delimiter to split the given string into substrings, and it’s optional. In case if the delimiter is not mentioned, by default, it will consider whitespace as a separator to split the strings.
  • maxsplit - It is useful to define the maximum number of splits, and it’s optional. By default, the maxsplit value is considered as -1 that means no limit on the number of splits.

String Split Method Example

Following is the example of splitting the given strings with required delimiters using the split() method in python.

 

msg1 = "Welcome to Tutlane"

msg2 = "Suresh, Rohini, Trishika"

msg3 = "Python$Java$C#"

lst1 = msg1.split()

lst2 = msg2.split(", ")

lst3 = msg3.split("$")

print(lst1)

print(lst2)

print(lst3)

If you observe the above example, we are splitting the strings with different delimiters using the split() method without specifying the maxsplit parameter.

 

When you execute the above python program, you will get the result like as shown below.

 

['Welcome', 'to', 'Tutlane']

['Suresh', 'Rohini', 'Trishika']

['Python', 'Java', 'C#']

In the above example, we didn’t mention any limit (maxsplit) on the number of splits so the split() method by default considered maxsplit as -1 and returned the list of all strings.

 

In case if you want to limit the number of strings split, you need to mention maxsplit parameter value in the split() method. If maxsplit specified, the split() method will return a maximum of maxsplit + 1 items.

 

Following is the example of splitting the string into a list by using the split() method and limit the number of strings split with maxsplit parameter value.

 

msg1 = "Suresh,Rohini,Trishika,Hanshith"

lst1 = msg1.split(",")

lst2 = msg1.split(",",1)

lst3 = msg1.split(",",2)

print(lst1)

print(lst2)

print(lst3)

If you observe the above example, we are limiting the string split by specifying the maxsplit parameter value in the split() method.

 

When you execute the above python program, you will get the result like as shown below.

 

['Suresh', 'Rohini', 'Trishika', 'Hanshith']

['Suresh', 'Rohini,Trishika,Hanshith']

['Suresh', 'Rohini', 'Trishika,Hanshith']

This is how you can use the string split() method in python to split the string into a list based on your requirements.