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 ".".
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 ".".
Just use str.rsplit
"xxx.yyy.zzz".rsplit('.', 1)[0]
'xxx.yyy'
"xxx.xxx.yyy.xyzxzy".rsplit('.', 1)[0]
'xxx.xxx.yyy'
print(s[:-s[::-1].find('.') - 1])