Skip to content
Home » String Searching Methods in Python

String Searching Methods in Python

Spread the love

Among the numerous string methods are formatting methods and statistical methods, which I covered in my articles not long ago. Today we’ll be talking about string searching methods.

searching methods

The find and rfind Methods

We can use the method find(str[, start, past]) to find the index at which the substring str is found in the string or in the slice between start and past, if these optional parameters are given. If the substring is not found, the method returns -1:

>>> text = "Find me if you can!"

Where is “me” in the string? It’s at index 5.

>>> text.find("me")
5

Where is “i” between index 6 and the end? It’s at index 8.

>>> text.find("i", 6)
8

Where is “you” between index 3 and 6? Nowhere in this slice.

>>> text.find("you", 3, 7)
-1

We have another method which works just like find. It’s rfind(str[, start, past]). The difference is it works backward. rfind returns the last index at which the substring is found or -1 if it’s not found. The optional start and past parameters work just the same:

>>> text = "He jumps. He catches the ball. He falls. He goes to hospital."
>>> print(text.rfind("."))   # the last period
60

Now let’s find the index of the last period between index 20 and 39, which corresponds to the substring ‘ the ball. He falls.’:

>>> print(text.rfind(".", 20, 40))
39

The index and rindex Methods

A very similar method is index(str[, start, past]). It works just like find, but if the substring is not found, a ValueError is raised:

>>> text = "Find me now!"
>>> text.index("me")
5

>>> text.index("me", 0, 4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found

There is also the rindex(str[, start, past]) method, which works just like index, but backward. It also raises an error if the substring is not found:

>>> text = "Take it or leave it, but don't bust it!"
>>> text.rindex("it")
36

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

The max and min Methods

The next two methods, max and min, return the maximum and  minimum alphabetical character respectively.  Each character in Unicode is represented by a number, hence the order.

>>> text = "PYTHON"
>>> max(text)
'Y'

>>> min(text)
'H'

>>> max("Python")
'y'

>>> min("Python")  # uppercase come before lowercase
'P'

>>> text = "I need 12 bottles."
>>> max(text)
't'

>>> min(text)  # spaces are earlier than digits and letters
' '

As for digits and letters, the order is: digits, uppercase letters, lowercase letters:

>>> max("123abcABC")  
'c'

>>> min("123abcABC")
'1'

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

Here’s the video version of the article:


Spread the love

Leave a Reply