Solved

Need to find the o,s in a python string

Posted on 2011-03-02
15
329 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
  • 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
New to Python, how do I import? 4 52
os.getenviron does not work when running script from crontab 10 56
Macports Import Problem 4 64
Windows Command Line in Python 16 73
This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
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 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…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

840 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