Finding text in a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moralesivan
    New Member
    • Jan 2009
    • 1

    Finding text in a string

    Code:
    import os, string, StringIO
    
    results = "lgqgdBddddddoS"
    index1 = results.find("B")
    index2 = results.find("S")
    	
    def extract(host, index1, index2):
    		return host.split(index1)[-1].split(index2)[0]
    how do i go about finding all the text between b and s. thakns
  • JCOSTA
    New Member
    • Jan 2009
    • 4

    #2
    Simple: just type something like
    print results[index1+1:index2]

    JC

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      JCOSTA is correct. It's called slicing (s[i:j]). Slicing can be used on sequence types, including lists, strings, and tuples.

      Comment

      Working...