Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
string
communitycreator

What is the string.find method in Python?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The find method in Python returns the index of the first occurrence of a substring in a string. If the substring is not present in the string, then -1 will be returned.

Syntax

string.find(substring [, start[, end]] )

Arguments

  • substring: String to be searched.

  • start: The index from which the substring search should start. This is an optional value, and by default the value is 0.

  • end: The index to which the substring search should happen. This is an optional value, and by default the value is the length of the string.

Return value

This is the first index at which the substring is found. If the substring is not present, -1 is returned.

Example

string = "JavaScript";
print("The index of Java in JavaScript is")
print(string.find("Java"));
print("\nThe index of Script in JavaScript is")
print(string.find("Script"));
print("\nThe searching for 'pt' in JavaScript from index 8")
print(string.find("pt", 8));
print("\nThe searching for 'va' in JavaScript from index 1 and 5")
print(string.find("va", 1,5));
print("\nThe searching for 'Python' in JavaScript")
print(string.find("Python"));

In the code above, we create a JavaScript string:

Index

string.find("Java")
  • We use the find method to check the index at which Java is present in the JavaScript string. We will get 0 as a result.

string.find("Script")
  • We use the find method to check the index at which Script is present in the string JavaScript. We will get 4 as a result.

string.find("pt", 8)
  • We use the find method to check the index at which pt is present in the JavaScript string from the 8th index. We will get 8 as a result.

string.find("va", 1,5)
  • We use the find method to check the index at which va is present in the string JavaScript between the 1 to 5 index. We will get 2 as a result.

print(string.find("Python"));  
  • We use the find method to check the index at which Python is present. We will get -1 because Python is not present in the JavaScript string.

RELATED TAGS

python
string
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring