{"id":24512,"date":"2021-10-21T10:04:18","date_gmt":"2021-10-21T04:34:18","guid":{"rendered":"https:\/\/python-programs.com\/?p=24512"},"modified":"2021-11-05T17:41:27","modified_gmt":"2021-11-05T12:11:27","slug":"python-string-rindex-method-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-string-rindex-method-examples\/","title":{"rendered":"Python String rindex() Method Examples"},"content":{"rendered":"
In the previous article, we have discussed Python String rfind() Method Examples<\/a> The rindex() method returns the location of the last occurrence of the specified value.<\/p>\n If the value is not found, the rindex() method throws an exception.<\/p>\n The rindex() method is nearly identical to the rfind() method.<\/p>\n Note:<\/strong> The only difference is rindex() method throws an exception if the value is not found whereas rfind() returns a value -1.<\/strong><\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n value:<\/strong>\u00a0This is required. The value to look for<\/p>\n start:<\/strong>\u00a0This is optional. Where to begin your search. The default value is 0.<\/p>\n end:<\/strong>\u00a0This is optional. Where to put an end to the search. The default value is to the end of the string.<\/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 rfind() Method Examples rindex() Function in Python: The rindex() method returns the location of the last occurrence of the specified value. If the value is not found, the rindex() method throws an exception. The rindex() method is nearly identical to the rfind() method. Note: The only …<\/p>\n
\nrindex() Function in Python:<\/strong><\/p>\nstring.rindex(value, start, end)<\/pre>\n
Given string = \"welcome to python learning platform\"\r\nGiven value = \"p\"<\/pre>\n
The result after applying rindex() function on the given string = 27<\/pre>\n
Given string = \"good morning btechgeeks\"\r\nGiven value = 'e'\r\nGiven start index = 3\r\nGiven end index = 16<\/pre>\n
The result after applying rindex() function on the given string = 15<\/pre>\n
\u00a0String rindex() Method Examples\u00a0in 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\"\r\n# Give the value as static input and store it in another variable.\r\ngvn_valu = \"p\"\r\n# Apply rindex() function to the given string for the given value that\r\n# returns the location of the last occurrence of the specified value.\r\n# Store it in another variable.\r\nrslt = gvn_str.rindex(gvn_valu)\r\n# Print the result after applying rindex() function on the given string\r\n# for the given value.\r\nprint(\"The result after applying rindex() function on the given string = \", rslt)\r\n# Similarly, do the same for the other values and print the result.\r\nrslt2 = gvn_str.rindex('o', 1, 8)\r\nrslt3 = gvn_str.rindex('m')\r\nprint(\"The result after applying rindex() function on the given string = \", rslt2)\r\nprint(\"The result after applying rindex() function on the given string = \", rslt3)\r\n<\/pre>\n
The result after applying rindex() function on the given string = 27\r\nThe result after applying rindex() function on the given string = 4\r\nThe result after applying rindex() function on the given string = 34<\/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 rindex() function to the given string for the given value that\r\n# returns the location of the last occurrence of the specified value.\r\n# Store it in another variable.\r\nrslt = gvn_str.rindex(gvn_valu)\r\n# Print the result after applying rindex() function on the given string\r\n# for the given value.\r\nprint(\"The result after applying rindex() function on the given string = \", rslt)\r\n# Similarly, do the same for the other value by giving the start and end positions\r\n# as user input using the int(input()) function and print the result.\r\ngvn_valu2 = input(\"Enter some random value = \")\r\ngvn_strtindx = int(input(\"Enter some random number = \"))\r\ngvn_endindx = int(input(\"Enter some random number = \"))\r\nrslt2 = gvn_str.rindex(gvn_valu2, gvn_strtindx, gvn_endindx)\r\nprint(\"The result after applying rindex() function on the given string = \", rslt2)\r\n<\/pre>\n
Enter some random string = good morning btechgeeks\r\nEnter some random value = o\r\nThe result after applying rindex() function on the given string = 6\r\nEnter some random value = e\r\nEnter some random number = 3\r\nEnter some random number = 16\r\nThe result after applying rindex() function on the given string = 15<\/pre>\n
\n