Search code examples
pythonstringsplitpunctuation

How to split a string by multiple punctuations with Python?


s = 'a,b,c d!e.f\ngood\tmorning&night'
delimiters = [',', '.', '!', '&', '', '\n', '\t']

s.split()

Can I split a string by all of ',', '.', '!', '&', ' ', '\n', '\t'? Is it possible to specify multiple delimiters for string.split()? For example, how can I split s into

['a','b','c','d','e','f','good','morning','night']

Solution

  • You can use regex to achieve this as:

    >>> import re
    >>> s = 'a,b,c d!e.f\ngood\tmorning&night'
    
    >>> re.split('[?.,\n\t&! ]', s)
    ['a', 'b', 'c', 'd', 'e', 'f', 'good', 'morning', 'night']
    

    If you are looking for a solution using split(), then here's a workaround:

    >>> identifiers = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~\n\t '
    
    >>> "".join((' ' if c in identifiers else c for c in s)).split()
    ['a', 'b', 'c', 'd', 'e', 'f', 'good', 'morning', 'night']
    

    Here, I am replacing all the identifiers with a space " " in the string, and then splitting the string based on the space.