C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python String find() MethodPython find() method finds substring in the whole string and returns index of the first match. It returns -1 if substring does not match. Signaturefind(sub[, start[, end]]) Parameters
Return TypeIf found it returns index of the substring, otherwise -1. Let's see some examples to understand the find() method. Python String find() Method Example 1An example of simple find method which takes only single parameter (a substring). # Python find() function example # Variable declaration str = "Welcome to the TheDeveloperBlog." # Calling function str2 = str.find("the") # Displaying result print(str2) Output: 11 Python String find() Method Example 2It returns -1 if not found any match, see the example. # Python find() function example # Variable declaration str = "Welcome to the TheDeveloperBlog." # Calling function str2 = str.find("is") # Displaying result print(str2) Output: -1 Python String find() Method Example 3Let's specify other parameters too and makes search more customize. # Python find() function example # Variable declaration str = "Welcome to the TheDeveloperBlog." # Calling function str2 = str.find("t") str3 = str.find("t",25) # Displaying result print(str2) print(str3) Output: 8 -1 Python String find() Method Example 4# Python find() function example # Variable declaration str = "Welcome to the TheDeveloperBlog." # Calling function str2 = str.find("t") str3 = str.find("t",20,25) # Displaying result print(str2) print(str3) Output: 8 24
Next TopicPython Strings
|