To get the substring within a string, we can use different functions like contains, find, index and in. In this article we will look at the code examples of all these methods and calculate the time complexity. This way you will know which method is good for overall code performance.

Code Example 1 – Using in

def in_(haystack, needle):
    return needle in haystack

print(in_("Captain America is the first Avenger", "first"))

#python #python string #python-short

Python String Substring, Contains, Find and index Comparison - Live Demo
1.05 GEEK