{"id":24770,"date":"2021-11-02T09:46:33","date_gmt":"2021-11-02T04:16:33","guid":{"rendered":"https:\/\/python-programs.com\/?p=24770"},"modified":"2021-11-05T19:16:35","modified_gmt":"2021-11-05T13:46:35","slug":"python-string-find-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-string-find-method-with-examples\/","title":{"rendered":"Python String find() Method with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python String expandtabs() Method with Examples<\/a> The find() method looks for the first instance of the specified value.<\/p>\n If the value cannot be found, the find() method returns -1.<\/p>\n The find() method is nearly identical to the index() method, with the exception that the index() method throws an exception if the value is not found.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n value:<\/strong> This is Required. It is a string. The value to look for in the string.<\/p>\n start:<\/strong>\u00a0This is optional. It is an integer. The starting point for the search. The default value is 0.<\/p>\n end:<\/strong>\u00a0This is optional. It is an integer. The ending point for the search. The default value is the end of the string.<\/p>\n Return Value:<\/strong><\/p>\n The find() method gives the following integer value:<\/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 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 Go through our tutorial and learn about various Python String Method Examples<\/a> and learn how to apply the knowledge while dealing with strings.<\/p>\n In the previous article, we have discussed Python String expandtabs() Method with Examples String find() Method in Python: The find() method looks for the first instance of the specified value. If the value cannot be found, the find() method returns -1. The find() method is nearly identical to the index() method, with the exception that …<\/p>\n
\nString find() Method in Python:<\/strong><\/p>\nstring.find(value, start, end)<\/pre>\n
\n
Given string = \"hello this is btechgeeks this\"\r\nGiven value = \"this \"<\/pre>\n
The index of the given value's{ this } first occurrence = 6<\/pre>\n
Given string = \"welcome to python programs to all\"\r\nGiven value = \"to\"\r\nstart position = 20\r\nend position = 30<\/pre>\n
The index of the given value's{ to } first occurrence in the given start and end positions :\r\n27<\/pre>\n
String find() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
1)Without giving start and end positions<\/h5>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = \"hello this is btechgeeks this\"\r\n# Give the value as static input and store it in another variable.\r\ngvn_valu = \"this\"\r\n# Pass the given value as an argument to the find() function for the given\r\n# string which returns the index of the given value's first occurrence if the\r\n# value exists and returns -1 if a value does not exist within the given string.\r\n# Store it in another variable.\r\nrslt = gvn_str.find(gvn_valu)\r\n# Print the index of the given value's first occurrence in the given string.\r\nprint(\r\n \"The index of the given value's{\", gvn_valu, \"} first occurrence = \", rslt)\r\n<\/pre>\n
The index of the given value's{ this } first occurrence = 6<\/pre>\n
2)With giving start and end positions<\/h5>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = \"welcome to python programs to all\"\r\n# Give the value as static input and store it in another variable.\r\ngvn_valu = \"to\"\r\n# Give the start position as static input and store it in another variable.\r\ngvn_strtpositn = 20\r\n# Give the end position as static input and store it in another variable.\r\ngvn_endpositn = 30\r\n# Pass the given value, start and end positions as arguments to the find()\r\n# function for the given string which returns the index of the given value's\r\n# first occurrence if the value exists and returns -1 if a value does not\r\n# exist within the given string in the given start and end range.\r\n# Store it in another variable.\r\nrslt = gvn_str.find(gvn_valu, gvn_strtpositn, gvn_endpositn)\r\n# Print the index of the given value's first occurrence in the given string.\r\nprint(\r\n \"The index of the given value's{\", gvn_valu, \"} first occurrence in the given start and end positions :\")\r\nprint(rslt)\r\n<\/pre>\n
The index of the given value's{ to } first occurrence in the given start and end positions :\r\n27<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
1)Without giving start and end positions<\/h5>\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 store it in another variable.\r\ngvn_valu = input(\"Enter some Random String(value) = \")\r\n# Pass the given value as an argument to the find() function for the given\r\n# string which returns the index of the given value's first occurrence if the\r\n# value exists and returns -1 if a value does not exist within the given string.\r\n# Store it in another variable.\r\nrslt = gvn_str.find(gvn_valu)\r\n# Print the index of the given value's first occurrence in the given string.\r\nprint(\r\n \"The index of the given value's{\", gvn_valu, \"} first occurrence = \", rslt)\r\n\r\n\r\n<\/pre>\n
Enter some Random String = good morning btechgeeks\r\nEnter some Random String(value) = e\r\nThe index of the given value's{ e } first occurrence = 15<\/pre>\n
2)With giving start and end positions<\/h5>\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 store it in another variable.\r\ngvn_valu = input(\"Enter some Random String(value) = \")\r\n# Give the start position as user input using the int(input()) function and \r\n# store it in another variable.\r\ngvn_strtpositn = int(input(\"Enter some random number = \"))\r\n# Give the end position as user input using the int(input()) function and \r\n# store it in another variable.\r\ngvn_endpositn = int(input(\"Enter some random number = \"))\r\n# Pass the given value, start and end positions as arguments to the find()\r\n# function for the given string which returns the index of the given value's\r\n# first occurrence if the value exists and returns -1 if a value does not\r\n# exist within the given string in the given start and end range.\r\n# Store it in another variable.\r\nrslt = gvn_str.find(gvn_valu, gvn_strtpositn, gvn_endpositn)\r\n# Print the index of the given value's first occurrence in the given string.\r\nprint(\r\n \"The index of the given value's{\", gvn_valu, \"} first occurrence in the given start and end positions :\")\r\nprint(rslt)\r\n<\/pre>\n
Enter some Random String = good morning all\r\nEnter some Random String(value) = o\r\nEnter some random number = 3\r\nEnter some random number = 10\r\nThe index of the given value's{ o } first occurrence in the given start and end positions :\r\n6<\/pre>\n
\n