{"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> 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 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 Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Are you facing difficulties in finding all the methods that a string object can call in python? Have a glance at this Python String Method Examples<\/a> Tutorial & meet such challenges with ease.<\/p>\n In the previous article, we have discussed Python String strip() Method Examples split() Method in Python: Splitting a string into a list is accomplished by the split() method. The separator can be specified; the default separator is any whitespace. Note: When maxsplit is specified, the list will have the number of elements specified plus one. …<\/p>\n
\nsplit() Method in Python:<\/strong><\/p>\nstring.split(separator, maxsplit)<\/pre>\n
Given string = \"welcome,to,python,programs\"\r\nGiven separator = \",\"\r\nGiven maxsplit value = 1<\/pre>\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
Given string = \"good morning this is btechgeeks\"<\/pre>\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
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the first string as static input and store it in a variable.\r\ngvn_fststr = \"welcome,to,python,programs\"\r\n# Give the separator as static input and store it in another variable.\r\ngvn_separtr = \",\"\r\n# Give the maxsplit as static input and store it in another variable.\r\nmaxsplt_valu = 1\r\n# Apply split() method to the given string for the given separator and\r\n# maxsplit() values which split a string into a list.\r\n# Store it in another variable.\r\nrslt_lst1 = gvn_fststr.split(gvn_separtr, maxsplt_valu)\r\n# Print the above given string\r\nprint(\"The above given first string is :\", gvn_fststr)\r\n# Print the above given string after applying split() function.\r\nprint(\"The given first string after applying split() function:\")\r\nprint(rslt_lst1)\r\n# Similarly do the same for other string without giving the separator and\r\n# maxsplit values and print the result list obtained.\r\ngvn_scndstr = \"good morning this is btechgeeks\"\r\nrslt_lst2 = gvn_scndstr.split()\r\nprint(\"The above given second string is :\", gvn_scndstr)\r\nprint(\"The given second string after applying split() function:\")\r\nprint(rslt_lst2)\r\n<\/pre>\n
The above given first string is : welcome,to,python,programs\r\nThe given first string after applying split() function:\r\n['welcome', 'to,python,programs']\r\nThe above given second string is : good morning this is btechgeeks\r\nThe given second string after applying split() function:\r\n['good', 'morning', 'this', 'is', 'btechgeeks']<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the first string as user input using the input() function and store it in a variable.\r\ngvn_fststr = input(\"Enter some random string = \")\r\n# Give the separator as user input using the input() function and store it in another variable.\r\ngvn_separtr = input(\"Enter some random separator = \")\r\n# Give the maxsplit as user input using the int(input()) function and store it in another variable. \r\nmaxsplt_valu = int(input(\"Enter some random number = \"))\r\n# Apply split() method to the given string for the given separator and\r\n# maxsplit() values which split a string into a list.\r\n# Store it in another variable.\r\nrslt_lst1 = gvn_fststr.split(gvn_separtr, maxsplt_valu)\r\n# Print the above given string\r\nprint(\"The above given first string is :\", gvn_fststr)\r\n# Print the above given string after applying split() function.\r\nprint(\"The given first string after applying split() function:\")\r\nprint(rslt_lst1)\r\n# Similarly do the same for other string without giving the separator and\r\n# maxsplit values and print the result list obtained.\r\ngvn_scndstr = input(\"Enter some random string = \")\r\nrslt_lst2 = gvn_scndstr.split()\r\nprint(\"The above given second string is :\", gvn_scndstr)\r\nprint(\"The given second string after applying split() function:\")\r\nprint(rslt_lst2)\r\n<\/pre>\n
Enter some random string = hello#this is btechgeeks\r\nEnter some random separator = #\r\nEnter some random number = 1\r\nThe above given first string is : hello#this is btechgeeks\r\nThe given first string after applying split() function:\r\n['hello', 'this is btechgeeks']\r\nEnter some random string = hello all good morning\r\nThe above given second string is : hello all good morning\r\nThe given second string after applying split() function:\r\n['hello', 'all', 'good', 'morning']<\/pre>\n
\n