Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8767673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:52:32+00:00 2026-06-13T16:52:32+00:00

Strings in Python have a find(somestring) method that returns the index number for somestring

  • 0

Strings in Python have a find(“somestring”) method that returns the index number for “somestring” in your string.

But let’s say I have a string like the following:

“$5 $7 $9 Total Cost: $35 $14”

And I want to find the index of the first occurrence of ‘$’ that occurs after the string “Total Cost” — I’d like to be able to tell python, search for ‘$’, starting at the index number for “Total Cost”, and return the index number (relative to the entire string) for the first occurrence of ‘$’ that you find. The find() method would return 0, and rfind() wouldn’t work either in this case.

One kind of kludgy way to do this is the following:

def findStrAfterStr(myString, searchText, afterText):

    splitString = myString.split(afterText)
    myIndex = len(splitString[0]) + len(afterText) + splitString[1].find(searchText)
    return myIndex

myString = "$5   $7    $9     Total Cost: $35   $14"
searchText = "$"
afterText = "Total Cost"

findStrAfterStr(myString, searchText, afterText)

But it seems like there should be an easier way to do this, and I assume there probably is and I just don’t know what it is. Thoughts?

This would be particular useful for slicing, when I find myself doing this a lot:

myString[myString.find("startingSubstr"):myString.find("endingSubstr")]

and naturally I want the “endingSubstr” to be the one that occurs after the “startingSubstr”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:52:33+00:00Added an answer on June 13, 2026 at 4:52 pm

    Use the optional second argument of str.find:

    def findStrAfterStr(myString, searchText, afterText):
        after_index = myString.index(afterText)
        return myString.find(searchText, after_index)
    

    Or, as pythonm suggests, you can use regexps.

    I recommend a “do I really need to” approach to regexps, because it’s often so hard to understand what the code does when you read it again later. Also I’ve found that in most cases you can do the same thing without regexp, and get code that’s easier to read in the bargain. Compare:

    import re
    
    def findStrAfterStr(myString, searchText, afterText):
        pattern = "{0}.*?({1})".format(re.escape(afterText), re.escape(searchText))
        match = re.search(pattern, myString)
        return match.start(1) if match else -1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a server side Python script that returns a JSON string containing parameters
My doc strings have references to other python classes that I've defined. Every time
I have a string in Python 2.7.2 say u\u0638. When I write it to
I have some code in a python string that contains extraneous empty lines. I
i have string stored in python variables, and i am outputting a html that
I have an array of strings in python which each string in the array
Does Python have a pool of all strings and are they (strings) singletons there?
Does Python have a built-in, simple way of encoding/decoding strings using a password? Something
I have about 50 million lists of strings in Python like this one: [1,
Is there a way to have multiline strings in VB.NET like Python a =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.