Important Notice:

Course Content
The Calendar Module
Library Function/Predefined Functions
0/4
Sys Module
Library Function/Predefined Functions
0/2
OS Module
Library Function/Predefined Functions
0/2
Functions in Python (Chapter-10) M3-R5
About Lesson

split():-

            The split() method splits a string into a list. You can specify the separator, default separator is any whitespace.

Syntax:-                       string.split(separator, maxsplit)

Note:-

separator

Optional. Specifies the separator to use when splitting the string. By default any whitespace is a separator

maxsplit

Optional. Specifies how many splits to do. Default value is -1, which is “all occurrences”

 

txt = “hello, my name is Peter, I am 26 years old”

x = txt.split(“, “)

print(x)

Output

[‘hello’, ‘my name is Peter’, ‘I am 26 years old’]

txt = “apple#banana#cherry#orange”

x = txt.split(“#”)

print(x)

Output

[‘apple’, ‘banana’, ‘cherry’, ‘orange’]

txt = “apple#banana#cherry#orange”

# setting the maxsplit parameter to 1, will return a list with 2 elements!

x = txt.split(“#”, 1)

print(x)

Output

[‘apple’, ‘banana#cherry#orange’]

error: Content is protected !!