Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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'
Comment

PREVIOUS NEXT
Code Example
Python :: python 3.8.5 download 32 bit 
Python :: Python Changing Directory 
Python :: create qr code in python 
Python :: create a dataframe from dict 
Python :: python pillow cut image in half 
Python :: what is module in python 
Python :: divisible in python 
Python :: how to add the sum of multiple columns into another column in a dataframe 
Python :: import python script from another directory 
Python :: np.arange and np.linspace difference 
Python :: string print in pattern in python 
Python :: how to reverse a string in python 
Python :: pandas count nans in column 
Python :: pythone csv 
Python :: convert 2d string array to float python 
Python :: how to make exe from.py file 
Python :: write a file python 
Python :: django admin override save 
Python :: How to know size of Python list 
Python :: how to square root in python 
Python :: argparse cli 
Python :: how to convert python input to int 
Python :: slicing in python listing 
Python :: convert timedelta to days 
Python :: python ordered dict to dict 
Python :: django authenticate 
Python :: python string contains 
Python :: python pillow convert jpg to png 
Python :: drop-trailing-zeros-from-decimal python 
Python :: get function in dictionary 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =