'Slice up to last occurrence of character in string

I want to slice string up to last occurrence of a specific character:

Example:

From the text "xxx.yyy.zzz" I want only "xxx.yyy"

From the text "xxx.xxx.yyy.xyzxzy" I want "xxx.xxx.yyy"

where I want to slice up to the last occurrence of ".".



Solution 1:[1]

Just use str.rsplit

"xxx.yyy.zzz".rsplit('.', 1)[0]
'xxx.yyy'

"xxx.xxx.yyy.xyzxzy".rsplit('.', 1)[0]
'xxx.xxx.yyy'

Solution 2:[2]

print(s[:-s[::-1].find('.') - 1])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 C.Nivs
Solution 2 Jenfi