Home > Blockchain >  Finding first word after punctuation
Finding first word after punctuation

Time:01-04

I want to find words the first word after the punctuation. I know that i am suppose to use .split() to do it, and i tried it out too, like this:

longstring = "Hello my name is ryan.We are new to python, and would like to learn more. So lets start."
newstring=longstring.split()

I want to get a list with this information:

['Hello', 'We', 'So']

Is there an efficient way?

CodePudding user response:

Split the sentence on punctuation

import re
text = " T?e  qu!ck ' brown 1 fox! jumps-.ver. the 'lazy' doG?  !"
sentences = re.split('(?<=[.!?])  ',text)

Split each sentence into words

for sentence in sentences:
    words = sentence.split()
    # Get the first word
    print(words[0]) 

You could then create a method to perform this for you.

CodePudding user response:

Here is a one line to do so with no regular expression

import string
print(' '.join(word.strip(string.punctuation) for word in longstring.split()))

but strongly recommend DogEatDog way for a simple reason .

in case of your longstring this way will not detect the ryan.We and keep it the same way , but if you have a space between like ryan. We then its all good .

the result with no space will be like the following

Hello my name is ryan.We are new to python and would like to learn more So lets start

CodePudding user response:

Your initial approach allready laid the groundwork for a concise solution.

# split longstring along punctuation
sentences = longstring.split(".")
# use list comprehension to split each sentence into words
new_strings = [sentence.split()[0] for sentence in sentences if sentence != '']

# the if-statement at the end is necessary to avoid splitting an empty sentence
  •  Tags:  
  • Related