Solved

Need to find the o,s in a python string

Posted on 2011-03-02
15
311 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 28

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 28

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IronPython and Visual Studio 2013+ 2 91
Is Converting Java classes to Python simple? 4 72
Get IP addres information from field 3 51
yara deployment 8 31
Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
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…

914 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now