Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: split string after a character

I have a string with two "-"

467.2-123-hdxdlfow

I want to remove everything after the second "-" so that I get "467.2-123". What is the best way to do this?

like image 718
SandyBr Avatar asked Nov 27 '22 14:11

SandyBr


1 Answers

before, sep, after = theString.rpartition("-")

This splits the str about the last occurrence of "-" and your answer would be the variable before.

like image 128
gibraltar Avatar answered Dec 19 '22 16:12

gibraltar