OriginalBrownster wrote: > Hi there: > > I was wondering if its at all possible to search through a string for a > specific character. > > I want to search through a string backwords and find the last > period/comma, then take everything after that period/comma > > Example > > If i had a list: bread, butter, milk > > I want to just take that last entry of milk. However the need for it > arises from something more complicated. > > Any help would be appreciated
The rfind() method of strings will search through a string for the first occurance of a substring, starting from the end. (find() starts from the beginning.) |>> s = "bread, butter, milk" |>> s.rfind(',') 13 |>> s.rfind('!') -1 |>> s[s.rfind(',') + 1:] ' milk' If you want to find either a period or comma you could do it like this: |>> i = max(s.rfind(ch) for ch in ',.') |>> i 13 |>> s[i + 1:] ' milk' Here's the output of help(s.rfind): Help on built-in function rfind: rfind(...) S.rfind(sub [,start [,end]]) -> int Return the highest 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. Enjoy Peace, ~Simon -- http://mail.python.org/mailman/listinfo/python-list