Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split string with limit by the end in Python

I have the following string:

"hello.world.foo.bar"

and I want to split (with the "." as delimiter, and only want to get two elemets starting by the end) this in the following:

["hello.world.foo", "bar"]

How can I accomplish this? exist the limit by the end?

like image 498
fj123x Avatar asked Dec 01 '13 13:12

fj123x


1 Answers

Use str.rsplit specifying maxsplit (the second argument) as 1:

>>> "hello.world.foo.bar".rsplit('.', 1) # <-- 1: maxsplit ['hello.world.foo', 'bar'] 
like image 53
falsetru Avatar answered Sep 16 '22 17:09

falsetru