python中的分区字符串并获取冒号后最后一段的值 [英] partition string in python and get value of last segment after colon

查看:101
本文介绍了python中的分区字符串并获取冒号后最后一段的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要得到这个例子中最后一个冒号后的值 1234567

I need to get the value after the last colon in this example 1234567

client:user:username:type:1234567

我不需要字符串中的任何其他内容,只需要最后一个 id 值.

I don't need anything else from the string just the last id value.

推荐答案

result = mystring.rpartition(':')[2]

如果您的字符串没有任何:,则结果将包含原始字符串.

If you string does not have any :, the result will contain the original string.

另一种应该慢一点的方法是:

An alternative that is supposed to be a little bit slower is:

result = mystring.split(':')[-1]

这篇关于python中的分区字符串并获取冒号后最后一段的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆