def hyphen_split(a): if a.count("-") == 1: return a.split("-")[0] return "-".join(a.split("-", 2)[:2]) >>> hyphen_split("some-sample-filename-to-split") 'some-sample' >>> hyphen_split("some-sample") 'some'