views:

278

answers:

5

In recent bash versions, I can do this:

$ string="Universe.World.Country.State.City.Street"
$ echo $string
Universe.World.Country.State.City.Street
$ newString="${string##*.}"
$ echo $newString
Street

Using Python, what is a succinct way to doing the same? I am interested in the last substring after the last period.

Thank you!

+3  A: 

How about

x[x.rfind('.') + 1 : ]

To me, that expresses what you're interested in (find the last dot, then take everything after it) more simply than a pattern or the concept of a "longest match".

Jon Skeet
@Downvoter: please leave a comment to explain... I'm a newbie at Python, so I'd really appreciate guidance if I'm doing something wrong.
Jon Skeet
I didn't downvote, but I'd imagine it looks too much like C and not python.
SilentGhost
This is pretty clever, but the `x.rpartition()` solution is the exact solution to the problem. In Python it is generally considered best to simply get what you want, rather than getting an index to what you want and then extract what you want; thus we prefer solutions that use `for x in some_list` rather than `for i in xrange(len(some_list))` and then using `some_list[i]` to access each item. But a downvote is unduly harsh!
steveha
A: 

Maybe:

re.search(r"\.([^.]*)$", s).groups()[0]

EDIT: First version was bad :)

Prody
A: 

If you know its always going to be the last element and a full stop you can't beat

"Universe.World.Country.State.City.Street".split(".")[-1]
Andrew Cox
Won't that go to all the trouble of finding *all* the dots, and create a whole list/array of strings, even though you know perfectly well you only want the last one?
Jon Skeet
Given that his string is only 6 elements long, I think that it would be eager optimisation to try anything that is harder to read.
Andrew Cox
+1  A: 
>>> "Universe.World.Country.State.City.Street".rsplit('.',1)[1]
'Street'

Edit: rpartition as suggested by SilentGhost seems to be the most efficient

# rpartition
$ python -m timeit -r100 -n100 -s 'x="Universe.World.Country.State.City.Street"' 'x.rpartition(".")[-1]'
100 loops, best of 100: 0.749 usec per loop

# rfind
$ python -m timeit -r100 -n100 -s 'x="Universe.World.Country.State.City.Street"' 'x[x.rfind(".")+1:]'
100 loops, best of 100: 0.808 usec per loop

# rsplit
$ python -m timeit -r100 -n100 -s 'x="Universe.World.Country.State.City.Street"' 'x.rsplit(".",1)[1]'
100 loops, best of 100: 0.858 usec per loop

# split
$ python -m timeit -r100 -n100 -s 'x="Universe.World.Country.State.City.Street"' 'x.split(".")[-1]'
100 loops, best of 100: 1.26 usec per loop

# regex
$ python -m timeit -r100 -n100 -s 'import re;rex=re.compile(r"\.([^.]*)$");x="Universe.World.Country.State.City.Street"' 'rex.search(x).groups()[0]'
100 loops, best of 100: 3.16 usec per loop
gnibbler
+3  A: 
>>> 'Universe.World.Country.State.City.Street'.rpartition('.')[2]
'Street'
SilentGhost
This is the fastest way anyone has posted so far
gnibbler
I like rpartition. It uses a python string lib. Easy to read. And seems analogous to the spirit of the bash operation. Everyone here is awesome! I learned so much more than just how to trim that string, too. Thanks! (The rsplit is appealing too)
duderonomy