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

How to extract all capitalised words, except for the first word in each sentence? Python

I have a paragraph (multiple sentences) and I want to catch all of the capitalised words in each sentence, except for the first word, even tough it is capitalised.

str = "He is a Good boy. When he sings Bob wants to listen. Bravo!"

Expected output:

>>> Good, Bob

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

>Solution :

import re

string = "He is a Good boy. When he sings Bob wants to listen. Bravo!"

print([i.strip() for i in re.findall(r'\b [A-Z][a-zA-Z]*\b',string)])

#output: ['Good', 'Bob']

strip() is for getting rid of white space
\b [A-Z]' – this part takes all not first capital letters in a word that not first in a sentence
[a-zA-Z]*\b' – this part takes any letters after that firs capital letter

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