Let’s check out some exercises that will help understand .split() method better.

Exercise 3-a: Splitting Up Words Using Split Method

Split the string based on space character: " ".


.split() method can be used to split strings based on a given character. It returns a list of split substrings.

lst = str.split(" ")

 

Exercise 3-b: Splitting up based on a specific character (:) with split method

Split the numbers.


.split() method can be used to split strings based on a given character. It returns a list of split substrings.

lst = str.split(":")

Exercise 3-c: Splitting based on special character (;)

Using the split method, split the string with semi colon (;) first. Then, print only the last element.


.split() method can be used to split strings based on a given character. It returns a list of split substrings.

lst = str.split(";")
ans_1 = lst[-1]