简体   繁体   中英

How to find the last occurring target letter in a string?

So there is a string and the question is asking me to find the last occurring target letter that I input. Then, I have to print string from the last occurring target letter that I got to the end of the string.

I only learned x.find() and x.rfind() , x.strip() , x.split() , and x.splitlines() .

Another condition is that the strings have to be converted into lowercase letters and string has two \n in the middle and at the end.

So far I have this:

text = text.strip().lower
target = input("target: ")

and I don't know where to go next.

This will return the text from the last target founded to the end of the text

text = text[text.rfind(target):]

The rfind will found the last occurrence of the target in a string and the slicing is taking the index that the target word start and will slice the text from that point until the end of the string since no number is following after the:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM