TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python String | find() method with Examples

Python String find() method with Examples on capitalize(), center(), count(), encode(), find(), format(), index(), join(), lower(), ljust(), isupper(), istitle(), isspace(), isprintable(), isnumeric(), islower() etc.

<< Back to PYTHON

Python String find() Method

Python find() method finds substring in the whole string and returns index of the first match. It returns -1 if substring does not match.

Signature

find(sub[, start[, end]])

Parameters

  • sub : substring
  • start : start index a range
  • end : last index of the range

Return Type

If found it returns index of the substring, otherwise -1.

Let's see some examples to understand the find() method.

Python String find() Method Example 1

An 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 2

It 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 3

Let'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




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf