{"id":24487,"date":"2021-10-21T10:04:17","date_gmt":"2021-10-21T04:34:17","guid":{"rendered":"https:\/\/python-programs.com\/?p=24487"},"modified":"2021-11-05T17:41:16","modified_gmt":"2021-11-05T12:11:16","slug":"python-string-rfind-method-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-string-rfind-method-examples\/","title":{"rendered":"Python String rfind() Method Examples"},"content":{"rendered":"
In the previous article, we have discussed Python Program for partition() Function<\/a> The rfind() method gives the position of the last occurrence of the specified value.<\/p>\n If the value cannot be found, the rfind() method returns -1.<\/p>\n The rfind() method is nearly identical to the rindex() method.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n value:<\/strong> This is required. The value to look for<\/p>\n start:<\/strong> This is optional. Where to begin your search. The default value is 0.<\/p>\n end:<\/strong> This 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 Program for partition() Function rfind() Function in Python: The rfind() method gives the position of the last occurrence of the specified value. If the value cannot be found, the rfind() method returns -1. The rfind() method is nearly identical to the rindex() method. Syntax: string.rfind(value, start, end) …<\/p>\n
\nrfind() Function in Python:<\/strong><\/p>\nstring.rfind(value, start, end)<\/pre>\n
Given string = \"welcome to python learning platform\"\r\nGiven value = \"p\"<\/pre>\n
The result after applying rfind() function on the given string = 27<\/pre>\n
Given string = \"welcome to python learning platform\"\r\nGiven value = \"o\"\r\nGiven start index = 1\r\nGiven end index = 8<\/pre>\n
The result after applying rfind() function on the given string = 4<\/pre>\n
String rfind() 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 rfind() function to the given string for the given value that gives the last\r\n# occurrence of the specified value.\r\n# Store it in another variable.\r\nrslt = gvn_str.rfind(gvn_valu)\r\n# Print the result after applying rfind() function on the given string\r\n# for the given value.\r\nprint(\"The result after applying rfind() function on the given string = \", rslt)\r\n# Similarly, do the same for the other values and print the result.\r\nrslt2 = gvn_str.rfind('o', 1, 8)\r\nrslt3 = gvn_str.rfind('b')\r\nprint(\"The result after applying rfind() function on the given string = \", rslt2)\r\nprint(\"The result after applying rfind() function on the given string = \", rslt3)\r\n<\/pre>\n
The result after applying rfind() function on the given string = 27\r\nThe result after applying rfind() function on the given string = 4\r\nThe result after applying rfind() function on the given string = -1<\/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 rfind() function to the given string for the given value that gives the last\r\n# occurrence of the specified value.\r\n# Store it in another variable.\r\nrslt = gvn_str.rfind(gvn_valu)\r\n# Print the result after applying rfind() function on the given string\r\n# for the given value.\r\nprint(\"The result after applying rfind() 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.rfind(gvn_valu2, gvn_strtindx, gvn_endindx)\r\nprint(\"The result after applying rfind() function on the given string = \", rslt2)\r\n<\/pre>\n
Enter some random string = hello this is btechgeeks\r\nEnter some random value = e\r\nThe result after applying rfind() function on the given string = 21\r\nEnter some random value = t\r\nEnter some random number = 1\r\nEnter some random number = 20\r\nThe result after applying rfind() function on the given string = 15<\/pre>\n
\n