{"id":24535,"date":"2021-10-22T09:58:32","date_gmt":"2021-10-22T04:28:32","guid":{"rendered":"https:\/\/python-programs.com\/?p=24535"},"modified":"2021-11-05T19:05:43","modified_gmt":"2021-11-05T13:35:43","slug":"python-string-split-method-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-string-split-method-examples\/","title":{"rendered":"Python String split() Method Examples"},"content":{"rendered":"

In the previous article, we have discussed Python String strip() Method Examples<\/a>
\nsplit() Method in Python:<\/strong><\/p>\n

Splitting a string into a list is accomplished by the split() method.<\/p>\n

The separator can be specified; the default separator is any whitespace.<\/p>\n

Note:<\/strong> When maxsplit<\/strong> is specified, the list will have the number of elements specified plus one.<\/p>\n

Syntax:<\/strong><\/p>\n

string.split(separator, maxsplit)<\/pre>\n

Parameters<\/strong><\/p>\n

separator:<\/strong> This is Optional. This parameter specifies the separator to be used when splitting the string. By default, any whitespace serves as a separator.<\/p>\n

maxsplit: <\/strong>This is Optional. Specifies the number of splits to perform. The default value is -1, which represents “all occurrences.”<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given string = \"welcome,to,python,programs\"\r\nGiven separator = \",\"\r\nGiven maxsplit value = 1<\/pre>\n

Output:<\/strong><\/p>\n

The above given string is : welcome,to,python,programs\r\nThe given string after applying split() function:\r\n['welcome', 'to,python,programs']<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given string = \"good morning this is btechgeeks\"<\/pre>\n

Output:<\/strong><\/p>\n

The above given string is : good morning this is btechgeeks\r\nThe given string after applying split() function:\r\n['good', 'morning', 'this', 'is', 'btechgeeks']<\/pre>\n

String split() Method Examples in Python<\/h2>\n