-2
s = 'I'm all right.'
s.split()

["I'm", "all", "right."]

Is there a convenient way to get rid of the periods when splitting a string into a list of words? For example, how can I obtain

["I'm", "all", "right"]

in the example above? I want to retain other punctuations such as en dash in right-out-of-school and single quote in I've been there.

Paw in Data
  • 1,262
  • 2
  • 14
  • 32
  • 2
    Assuming that you want the split working across multiple sentences, just do ```re.sub('\.\s*', ' ', s).split()``` where re is regex library – Ehtesham Siddiqui Dec 28 '20 at 12:56
  • @EhteshamSiddiqui Thank you! This is exactly what I'm looking for! You can make it an answer if you want, then I'll accept it. – Paw in Data Dec 28 '20 at 12:59

1 Answers1

1

You can use str.replace() before you split.

s = 'I\'m all right.'
out = s.replace('.', '').split()

print(out)

Output:

["I'm", 'all', 'right']
solid.py
  • 2,782
  • 5
  • 23
  • 30