Solved

Need to find the o,s in a python string

Posted on 2011-03-02
15
341 Views
Last Modified: 2012-05-11
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
Comment
Question by:megaapps
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 2
15 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 35018305
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
 
LVL 41

Expert Comment

by:HonorGod
ID: 35018420
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
 

Author Comment

by:megaapps
ID: 35018462
I just need to find the o not the s
0
Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

 
LVL 41

Accepted Solution

by:
HonorGod earned 500 total points
ID: 35018617
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
 

Author Comment

by:megaapps
ID: 35018698
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
 

Author Comment

by:megaapps
ID: 35018702
By the else statement
0
 

Author Comment

by:megaapps
ID: 35018721
Well I fixed that but now it is saying that the return statement is outside of the function
0
 

Author Closing Comment

by:megaapps
ID: 35018744
Thanks I got it all fixed now.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35019193
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
 
LVL 41

Expert Comment

by:HonorGod
ID: 35019277
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
 
LVL 29

Expert Comment

by:pepr
ID: 35019354
No semicolons, please! ;)  You may use it personally, but do not spoil the beginners.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35019397
old habit, like old programmers, die hard...

Is this semi-colon ok?   ;-)
0
 

Author Comment

by:megaapps
ID: 35019583
Thanks Honor everything worked good.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35019847
My pleasure.  Glad to be of assistance.
0
 
LVL 29

Expert Comment

by:pepr
ID: 35019967
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question