Have to try

Wayne Pierce wayne at mishre.com
Sat Feb 16 22:53:30 EST 2002


Rick,

Rick Hamilton wrote:
> OK - I know I am going to get fried here but - I am out of options. I'm 
> a programmer (Java, VB and C++) and am taking a class on Operating 
> systems. The nut teaching this course is asking us to do a ton on 
> programming in Python. OK -

   You've come to the wrong newsgroup if you want to get flamed, sorry. 
  Is the professor a nut because he's using Python?  Or does he go 
galloping around the room with two coconut halves talking about swallows?

> So I am trying to do a (what should be) simple thing. I have a string 
> stored in a variable. I want to search that string for the occurrence of 
> a sub string. I have tried
> 
> Find(s,sub)
> 
> Rfind
> 
> MyString.find(sub)

Let's say you have a variable "var" with a value of "my string"

var = "my string"

If you are using a recent version of Python you can do the following:

 >>> var.find("tr")
4

Since var is a string variable all the string functions can be accessed 
directly using dot-notation.  The documentation on the web site for the 
String module is the older way of doing it.  To use that you would need 
to type:

import string

Before you could use the functions, then to use them you would type:

 >>> string.find(var, "tr")
4

> Nothing - I have not yet figured out the appropriate way to treat a 
> string and am heading for a very weak book that we have as a reference. 
> Anyone will to toss a fellow programmer a helping had I'm willing to get 
> flamed to get this over with.

If all else fails try the following:

 >>> dir()
['__builtins__', '__doc__', '__name__', 'var']

(this is what mine shows)

This is your current namespace, if you wanted to see information about 
var you could type:

 >>> dir(var)
['capitalize', 'center', 'count', 'encode', 'endswith', 'expandtabs', 
'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 
'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'replace', 
'rfind', 'rindex', 'rjust', 'rstrip', 'split', 'splitlines', 
'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper']

To use any of these you can just type var.<function> .

TTFN,

Wayne





More information about the Python-list mailing list