{"id":24523,"date":"2021-10-21T10:04:25","date_gmt":"2021-10-21T04:34:25","guid":{"rendered":"https:\/\/python-programs.com\/?p=24523"},"modified":"2021-11-05T18:26:56","modified_gmt":"2021-11-05T12:56:56","slug":"python-string-rsplit-method-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-string-rsplit-method-examples\/","title":{"rendered":"Python String rsplit() Method Examples"},"content":{"rendered":"
In the previous article, we have discussed Python String rpartition() Method Examples<\/a> Starting from the right, the rsplit() method splits a string into a list.<\/p>\n If no “max” parameter is specified, this method will return the same value as split().<\/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 Note:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:\u00a0<\/strong><\/p>\n \u00a0<\/strong>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 rpartition() Method Examples rsplit() Method in Python: Starting from the right, the rsplit() method splits a string into a list. If no “max” parameter is specified, this method will return the same value as split(). Note: When maxsplit is specified, the list will have the number …<\/p>\n
\n rsplit() Method in Python:<\/strong><\/p>\nstring.rsplit(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 rsplit() 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 rsplit() function:\r\n['good', 'morning', 'this', 'is', 'btechgeeks']<\/pre>\n
If you don't give any separator and maxsplit values it takes space,-1 values\r\nrespectively by default.<\/pre>\n
String rsplit() 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 rsplit() method to the given string for the given separator and\r\n# maxsplit() which splits a string into a list starting from the right.\r\n# Store it in another variable.\r\nrslt_str1 = gvn_fststr.rsplit(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 rsplit() function.\r\nprint(\"The given first string after applying rsplit() function:\")\r\nprint(rslt_str1)\r\n# Similarly do the same for other string without giving the separator and maxsplit values\r\n# and print the result list obtained.\r\ngvn_scndstr = \"good morning this is btechgeeks\"\r\nrslt_str2 = gvn_scndstr.rsplit()\r\nprint(\"The above given second string is :\", gvn_scndstr)\r\nprint(\"The given second string after applying rsplit() function:\")\r\nprint(rslt_str2)\r\n<\/pre>\n
The above given first string is : welcome,to,python,programs\r\nThe given first string after applying rsplit() 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 rsplit() 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 rsplit() method to the given string for the given separator and\r\n# maxsplit() which splits a string into a list starting from the right.\r\n# Store it in another variable.\r\nrslt_str1 = gvn_fststr.rsplit(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 rsplit() function.\r\nprint(\"The given first string after applying rsplit() function:\")\r\nprint(rslt_str1)\r\n# Similarly do the same for other string without giving the separator and maxsplit values\r\n# and print the result list obtained.\r\ngvn_scndstr = input(\"Enter some random string = \")\r\nrslt_str2 = gvn_scndstr.rsplit()\r\nprint(\"The above given second string is :\", gvn_scndstr)\r\nprint(\"The given second string after applying rsplit() function:\")\r\nprint(rslt_str2)<\/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 rsplit() function:\r\n['hello this is', 'btechgeeks']\r\nEnter some random string = hello all goodmorning####\r\nThe above given second string is : hello all goodmorning####\r\nThe given second string after applying rsplit() function:\r\n['hello', 'all', 'goodmorning####']<\/pre>\n
\n