[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 358
  • Last Modified:

Need to find the o,s in a python string

Hi all I need to find all instances if o,s in the string that is below. But I need to only use the find function to do it. Like text.find. I know I need a loop to iterate through the text but when I try to add the for loop it gives me an error.
text = "Who's on first?"

Open in new window

0
megaapps
Asked:
megaapps
  • 7
  • 6
  • 2
1 Solution
 
HonorGodCommented:
Like this?  Or something else?
>>> text = "Who's on first?"
>>> for c in text :
...   if c == 'o' or c == 's' :
...     print c
...
o
s
o
s
>>>

Open in new window

0
 
HonorGodCommented:
If you must use find(), maybe something like this:
text = "Who's on first?"
offset = -1
while True :
  o = text.find( 'o', offset + 1 )
  s = text.find( 's', offset + 1 )
  if o > -1 and s > -1 :
    offset = min( o, s );
    print offset, text[ offset ];
  elif o > -1 or s > -1 :
    offset = max( o, s );
    print offset, text[ offset ];
  else :
    break;

Open in new window

0
 
megaappsAuthor Commented:
I just need to find the o not the s
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
HonorGodCommented:
oh, how's this?
>>> text = "Who's on first?"
>>> offset = -1
>>> while True :
...   offset = text.find( 'o', offset + 1 );
...   if offset > -1 :
...     print offset
...   else :
...     break;
...
2
6
>>>

Open in new window

0
 
megaappsAuthor Commented:
Of if I am trying to use this in a function I am getting a error saying invalid syntax
def findTheO():
    text = "Who's on first?"
    offset = -1
    while True:
        offset = text.find('o',offset + 1);
        if offset > -1:
            print offset
            else:
                break;
   return

Open in new window

0
 
megaappsAuthor Commented:
By the else statement
0
 
megaappsAuthor Commented:
Well I fixed that but now it is saying that the return statement is outside of the function
0
 
megaappsAuthor Commented:
Thanks I got it all fixed now.
0
 
HonorGodCommented:
Super.  I didn't see your updates until after you had accepted the answer! ;-)

Thanks for the grade & points.

Good luck & have a great day.
0
 
HonorGodCommented:
In general, however, I would pass the string, and the character to be found as parameters...

Something like this, perhaps:
def findChar( text, char ) :
  result = [];
  offset = -1;
  while True:
    offset = text.find( char, offset + 1 );
    if offset > -1:
      result.append( offset );
    else:
      break;
  return result;

print findChar( "Who's on first?", 'o' );

Open in new window

0
 
peprCommented:
No semicolons, please! ;)  You may use it personally, but do not spoil the beginners.
0
 
HonorGodCommented:
old habit, like old programmers, die hard...

Is this semi-colon ok?   ;-)
0
 
megaappsAuthor Commented:
Thanks Honor everything worked good.
0
 
HonorGodCommented:
My pleasure.  Glad to be of assistance.
0
 
peprCommented:
To HonorGod: Yes, some semicolons are just fine :)))  I do understand you.  I hope you feel that I did not want to attack you ;)

[Stop kidding (for myself) and leave this question R.I.P.]
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now