Find a substring withing a string...
Maurix
maurix78_remove_this_ at wanadoo.es
Sat Jun 21 10:35:29 EDT 2003
You don't need the find() function, you need the dir() (and help() in
the last versions) function and play with the interactive interpreter,
try this:
>python #(2.1 is the older that i have, i don't have 1.5 sorry)
>>> dir()
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError',
'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError',
'Exception', 'FloatingPointError', 'IOError', 'ImportError',
'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt',
'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented',
'NotImplementedError', 'OSError', 'OverflowError', 'RuntimeError',
'RuntimeWarning', 'StandardError', 'SyntaxError', 'SyntaxWarning',
'SystemError', 'SystemExit', 'TabError', 'TypeError',
'UnboundLocalError', 'UnicodeError', 'UserWarning', 'ValueError',
'Warning', 'ZeroDivisionError', '__debug__', '__doc__', '__import__',
'__name__', 'abs', 'apply', 'buffer', 'callable', 'chr', 'cmp',
'coerce', 'compile', 'complex', 'copyright', 'credits', 'delattr',
'dir', 'divmod', 'eval', 'execfile', 'exit', 'filter', 'float',
'getattr', 'globals', 'hasattr', 'hash', 'hex', 'id', 'input', 'int',
'intern', 'isinstance', 'issubclass', 'len', 'license', 'list',
'locals', 'long', 'map', 'max', 'min', 'oct', 'open', 'ord', 'pow',
'quit', 'range', 'raw_input', 'reduce', 'reload', 'repr', 'round',
'setattr', 'slice', 'str', 'tuple', 'type', 'unichr', 'unicode', 'vars',
'xrange', 'zip']
# nothing good here... let's try a good module...
>>> import String
#... it don't work... ummm it may be...
>>> import string
>>> dir(string)
['_StringType', '__builtins__', '__doc__', '__file__', '__name__',
'_float', '_idmap', '_idmapL', '_int', '_long', 'atof', 'atof_error',
'atoi', 'atoi_error', 'atol', 'atol_error', 'capitalize', 'capwords',
'center', 'count', 'digits', 'expandtabs', 'find', 'hexdigits', 'index',
'index_error', 'join', 'joinfields', 'letters', 'ljust', 'lower',
'lowercase', 'lstrip', 'maketrans', 'octdigits', 'printable',
'punctuation', 'replace', 'rfind', 'rindex', 'rjust', 'rstrip', 'split',
'splitfields', 'strip', 'swapcase', 'translate', 'upper', 'uppercase',
'whitespace', 'zfill']
# Umm there is a "find" function let's tray it
>>> find
>>> string.find("a","a b c")
-1
#Umm... no, maybe is...
>>> string.find("a b c","a")
0
>>> string.find("a b c","b")
2
# Yes! exactly what i need!!
# Here with 2.1 i can make... (i don't know in 1.5)
>>> print string.find.__doc__
find(s, sub [,start [,end]]) -> in
Return the lowest index in s where substring sub is found,
such that sub is contained within s[start,end]. Optional
arguments start and end are interpreted as in slice notation.
Return -1 on failure.
# in 2.2 you will make:
>>> help(string.find)
and surely in new versions you can use the string object
and search in the object members:
>>> dir("")
>>> help("".find)
This is the advantage of use an interpreter, make this with c if you can!
The only problem that i have with this is that i find too interesting
things and i don't make my job! ;-)
And maybe you can jump to www.python.org upgrade to python2.2 and reed
the tutorial http://www.python.org/doc/current/tut/tut.html
FooBar wrote:
> OK I will be the first to admit that I am VERY new to Pythin and
> know little of the syntax of this language. I want to perform the
> simple task of testing to see if a substring exists within string. I
> think the syntax is find(sting,substring) and the return is -1 if the
> substirng is not found otherwise the start position of the substring in
> the string. is this correct?
>
> Moreover is this code totally screwed?
>
>
> takeaction = find(stringvar,'LITERALSUB')
> if takeaction != -1:
> action_to_take_if_found
> else:
> action_tp_take_if_not_found
>
>
> Any help with this is appreciated as I am a total DFU when it
> comes to this for now anyway...
>
>
> Thanks,
> DB
>
>
>
>
More information about the Python-list
mailing list