String Formatting

O

OriginalBrownster

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
 
S

Sybren Stuvel

OriginalBrownster enlightened us with:
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.

string = "bread, butter, milk"
parts = string.split(', ')
print parts[-1]

Sybren
 
P

Pontus Ekberg

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
s='bread, butter, milk'
s.rsplit(',', 1)[-1] ' milk'
s.rsplit(',', 1)[-1].strip()
'milk'


Hope that helps.
 
A

Avell Diroll

OriginalBrownster said:
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



Would that work for you ?
a = 'bread, butter, milk'
a 'bread, butter, milk'
b = a.split(',')
b ['bread', ' butter', ' milk']
c = b[-1]
c ' milk'
d = c.strip()
d
'milk'




HIH


Avell
 
S

Simon Forman

OriginalBrownster said:
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
 
J

John Machin

OriginalBrownster said:
Hi there:

I was wondering if its at all possible to search through a string for a
specific character.

Don't wonder; read the tutorials, read the manuals, and ponder the
sheer uselessness of any computer language that offered neither such a
facility nor the means to build it yourself.
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.

Python terminology: that's not a list, it's a string.
Any help would be appreciated

If you already know that you are looking for a comma, then the
following will do the job. If you know that you are looking for a
period, make the obvious substitution.
x = " bread, butter, milk "
x.split(",") [' bread', ' butter', ' milk ']
x.split(",")[-1] ' milk '
x.split(",")[-1].strip() 'milk'
x = " no commas at all "
x.split(",") [' no commas at all ']
x.split(",")[-1] ' no commas at all '
x.split(",")[-1].strip() 'no commas at all'

*HOWEVER* if you really mean what you said (i.e. start at the
rightmost, go left until you strike either a comma or a period,
whichever comes first) then you need something like this:
.... return s[max(s.rfind(','), s.rfind('.')) + 1:]
....
The serendipity in the above is that if the sought character is not
found, rfind() returns -1 which fits in nicely without the need for an
"if" statement to do something special.

HTH,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top