Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Splitting string into words and space and punctuation

inp = "Hello world!"

and I expect something like that ["Hello"," ","world","!"]

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

using itertools.groupby

You can use itertools.groupby and a check with str.isalpha (which you can easily adapt to other character classes if needed):

s = "Hello world!"

from itertools import groupby
out = [''.join(g) for _,g in groupby(s, str.isalpha)]

output: ['Hello', ' ', 'world', '!']

using a regex

You can also use a regex and split on the word boundaries (dropping the first item that is the sentence start):

s = "Hello world!"

import re
out = re.split(r'\b', s)[1:]

output: ['Hello', ' ', 'world', '!']

NB. don’t use input as variable name, this is a python builtin

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading