Python split a string at an underscore

the following statement will split name into a list of strings

a=name.split("_")

you can combine whatever strings you want using join, in this case using the first two words

b="_".join(a[:2])
c="_".join(a[2:])

maybe you can write a small function that takes as argument the number of words (n) after which you want to split

def func(name, n):
    a=name.split("_")
    b="_".join(a[:n])
    c="_".join(a[n:])
    return [b,c]

Leave a Comment