Solved

Need to find the o,s in a python string

Posted on 2011-03-02
15
334 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
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…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

749 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