Answers for "split at the second occurrence of the element python"

0

split at the second occurrence of the element python

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'
Posted by: Guest on October-17-2021

Code answers related to "split at the second occurrence of the element python"

Python Answers by Framework

Browse Popular Code Answers by Language