{"id":24519,"date":"2021-10-21T10:04:22","date_gmt":"2021-10-21T04:34:22","guid":{"rendered":"https:\/\/python-programs.com\/?p=24519"},"modified":"2021-11-05T18:26:31","modified_gmt":"2021-11-05T12:56:31","slug":"python-string-rpartition-method-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-string-rpartition-method-examples\/","title":{"rendered":"Python String rpartition() Method Examples"},"content":{"rendered":"
In the previous article, we have discussed Python String rjust() Method Examples<\/a> The rpartition() method looks for the last occurrence of a string and splits it into a tuple with three elements.<\/p>\n The part preceding the specified string is contained in the first element.<\/p>\n The specified string is contained in the second element.<\/p>\n The part following the string is contained in the third element.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n value:<\/strong>\u00a0This is required. The string to look for.<\/p>\n If the specified value is not found, the rpartition() method returns a tuple containing the following values: 1 – an empty string, 2 – an empty string, 3 – the entire string.<\/p>\n For example:<\/strong><\/p>\n Output:<\/strong><\/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 rjust() Method Examples rpartition() Method in Python: The rpartition() method looks for the last occurrence of a string and splits it into a tuple with three elements. The part preceding the specified string is contained in the first element. The specified string is contained in the …<\/p>\n
\nrpartition() Method in Python:<\/strong><\/p>\nstring.rpartition(value)<\/pre>\n
gvn_str = \"welcome to python learning platform\"\r\nrslt_tupl = gvn_str.rpartition(\"java\")\r\nprint(rslt_tupl)\r\n<\/pre>\n
('', '', 'welcome to python learning platform')<\/pre>\n
Given string = \"welcome to python learning platform, python programs\"\r\nGiven value = \"python\"<\/pre>\n
The result tuple after applying rpartition() function on the given string : \r\n('welcome to python learning platform, ', 'python', ' programs')<\/pre>\n
Given string = \"good morning all\"\r\nGiven value = \"all\"<\/pre>\n
The result tuple after applying rpartition() function on the given string : \r\n('good morning ', 'all', '')<\/pre>\n
String rpartition() Method Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = \"welcome to python learning platform, python programs\"\r\n# Give the value as static input and store it in another variable.\r\ngvn_valu = \"python\"\r\n# Apply rpartition() function to the given string to search for the last\r\n# occurrence of a string and splits it into a tuple with three elements\r\n# as mentioned above.\r\n# Store it in another variable.\r\nrslt_tupl = gvn_str.rpartition(gvn_valu)\r\n# Print the result tuple after applying rpartition() function on the given string\r\n# for the given value\r\nprint(\"The result tuple after applying rpartition() function on the given string : \")\r\nprint(rslt_tupl)\r\n# Similarly, do the same for the other string and print the result string\r\ngvn_str2 = \"good morning all\"\r\ngvn_valu2 = \"all\"\r\nrslt_tupl2 = gvn_str2.rpartition(gvn_valu2)\r\nprint(\"The result tuple after applying rpartition() function on the given second string : \")\r\nprint(rslt_tupl2)\r\n<\/pre>\n
The result tuple after applying rpartition() function on the given string : \r\n('welcome to python learning platform, ', 'python', ' programs')\r\nThe result tuple after applying rpartition() function on the given second string : \r\n('good morning ', 'all', '')<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngvn_str = input(\"Enter some random string = \")\r\n# Give the value as user input using the input() function and\r\n# store it in another variable.\r\ngvn_valu = input(\"Enter some random value = \")\r\n# Apply rpartition() function to the given string to search for the last\r\n# occurrence of a string and splits it into a tuple with three elements\r\n# as mentioned above.\r\n# Store it in another variable.\r\nrslt_tupl = gvn_str.rpartition(gvn_valu)\r\n# Print the result tuple after applying rpartition() function on the given string\r\n# for the given value\r\nprint(\"The result tuple after applying rpartition() function on the given string : \")\r\nprint(rslt_tupl)\r\n# Similarly, do the same for the other string and print the result string\r\ngvn_str2 = input(\"Enter some random string = \")\r\ngvn_valu2 = input(\"Enter some random value = \")\r\nrslt_tupl2 = gvn_str2.rpartition(gvn_valu2)\r\nprint(\"The result tuple after applying rpartition() function on the given second string : \")\r\nprint(rslt_tupl2)<\/pre>\n
Enter some random string = hello this is btechgeeks hello\r\nEnter some random value = hello\r\nThe result tuple after applying rpartition() function on the given string : \r\n('hello this is btechgeeks ', 'hello', '')\r\nEnter some random string = good morning all this is python programming\r\nEnter some random value = good\r\nThe result tuple after applying rpartition() function on the given second string : \r\n('', 'good', ' morning all this is python programming')<\/pre>\n
\n