I need to get the value after the last colon in this example 1234567
client:user:username:type:1234567
I don't need anything else from the string just the last id value.
Use the str. split() method to split a string on the colons, e.g. my_list = my_str. split(':') .
Python String | split()split() method in Python split a string into a list of strings after breaking the given string by the specified separator. Parameters : separator : This is a delimiter. The string splits at this specified separator.
Use the str. rsplit() method with maxsplit set to 1 to split a string and get the last element. The rsplit() method splits from the right and will only perform a single split when maxsplit is set to 1 .
In Python, the str[0:n] option extracts a substring from a string. We may need to acquire the string that occurs after the substring has been found in addition to finding the substring.
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]
foo = "client:user:username:type:1234567" last = foo.split(':')[-1]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With