join – python – split and get the part of a str before the last occurrence of separator

I am trying to extract certain part of some strings. These strings look like

a = "1_2_3"
b = "1_2_3_4"

I would want to get everything of each string before the last underscore so:


I thought this line should work:


but it gives me the error: AttributeError: ‘str’ object has no attribute ‘joint’

However, apparently, a.split("_")(:-1) is not a str but a list (I checked by type(a.split("_")(:-1))). Does anyone know why this happens and how to solve it?