Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 359
  • 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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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