Skip to content
Advertisement

Partition string in python and get all the value before colon

I have to get all the values before the last colon(:) i.e. client:user:username:type from the following string:

Input: client:user:username:type:1234567 Output: client:user:username:type

I know I can split by : and then iterate and append all the tokens before :.

But is there a better pythonic way to achieve this?

Advertisement

Answer

just find the index of last : and then extract the rest of string base on that

JavaScript

output

JavaScript

as suggested by @muzzletov, you can use str.rindex and then do string slicing

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement