0
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']
Paw in Data
  • 1,262
  • 2
  • 14
  • 32
  • 3
    I think it would be better if you can use regex for this one. Possible duplicate of [https://stackoverflow.com/questions/4998629/split-string-with-multiple-delimiters-in-python](https://stackoverflow.com/questions/4998629/split-string-with-multiple-delimiters-in-python)? – c21253 Dec 30 '20 at 17:06

1 Answers1

2

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.

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126