Python Reverse Find in String

2024/11/20 23:17:04

I have a string and an arbitrary index into the string. I want find the first occurrence of a substring before the index.

An example: I want to find the index of the 2nd I by using the index and str.rfind()

s = "Hello, I am 12! I like plankton but I don't like Baseball."
index = 34 #points to the 't' in 'but'
index_of_2nd_I = s.rfind('I', index)
#returns = 36 and not 16 

Now I would expect rfind() to return the index of the 2nd I (16) but it returns 36. after looking it up in the docs I found out rfind does not stand for reverse find.

I'm totally new to Python so is there a built in solution to reverse find? Like reversing the string with some python [::-1] magic and using find, etc? Or will I have to reverse iterate char by char through the string?

Answer

Your call tell rfind to start looking at index 34. You want to use the rfind overload that takes a string, a start and an end. Tell it to start at the beginning of the string (0) and stop looking at index:

>>> s = "Hello, I am 12! I like plankton but I don't like Baseball."
>>> index = 34 #points to the 't' in 'but'
>>> index_of_2nd_I = s.rfind('I', 0, index)
>>>
>>> index_of_2nd_I
16
https://en.xdnf.cn/q/26267.html

Related Q&A

Is there a direct approach to format numbers in jinja2?

I need to format decimal numbers in jinja2. When I need to format dates, I call the strftime() method in my template, like this:{{ somedate.strftime(%Y-%m-%d) }}I wonder if there is a similar approach …

Why would running scheduled tasks with Celery be preferable over crontab?

Considering Celery is already a part of the stack to run task queues (i.e. it is not being added just for running crons, that seems an overkill IMHO ).How can its "periodic tasks" feature be …

use a css stylesheet on a jinja2 template

I am making a website using html, css, flask and jinja2.I have a page working on a flask server, the buttons and labels etc. are displayed, but the css stylesheet I have is not loaded in.How would I li…

How to extend Python class init

I have created a base class:class Thing():def __init__(self, name):self.name = nameI want to extend the class and add to the init method so the that SubThing has both a name and a time property. How d…

type hint for an instance of a non specific dataclass

I have a function that accepts an instance of any dataclass. what would be an appropriate type hint for it ?havent found something official in the python documentation this is what I have been doing, …

Is there a static constructor or static initializer in Python?

Is there such a thing as a static constructor in Python? How do I implement a static constructor in Python?Here is my code... The __init__ doesnt fire when I call App like this. The __init__ is not…

What does this - in jinja2 template engine do?

I am learning jinja2 because Google App Engine recommends it.I found this example on Wikipedia: http://en.wikipedia.org/wiki/Jinja_%28template_engine%29{%- for item in item_list %}{{ item }}{% if not l…

When to apply(pd.to_numeric) and when to astype(np.float64) in python?

I have a pandas DataFrame object named xiv which has a column of int64 Volume measurements. In[]: xiv[Volume].head(5) Out[]: 0 252000 1 484000 2 62000 3 168000 4 232000 Name: Volume, d…

How to change folder names in python?

I have multiple folders each with the name of a person, with the first name(s) first and the surname last. I want to change the folder names so that the surname is first followed by a comma and then t…

Python return list from function

I have a function that parses a file into a list. Im trying to return that list so I can use it in other functions. def splitNet():network = []for line in open("/home/tom/Dropbox/CN/Python/CW2/net…