KnowledgeBoat Logo
|

Computer Science

Explain function split() with an example.

Python String Manipulation

3 Likes

Answer

The split() function breaks up a string at the specified separator and returns a list of substrings. The syntax is str.split([separator, [maxsplit]]). The split() method takes a maximum of 2 parameters:

  1. separator (optional) — The separator is a delimiter. The string splits at the specified separator. If the separator is not specified, any whitespace (space, newline, etc.) string is a separator.

  2. maxsplit (optional) — The maxsplit defines the maximum number of splits. The default value of maxsplit is -1, which means no limit on the number of splits.

For example,

x = "blue;red;green"
print(x.split(";"))
Output
['blue', 'red', 'green']

Answered By

2 Likes


Related Questions