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

Python – Separate last sentence from string in a list

I have a string like

x = 'Test. Test1. Test test test.'

Now I would like to separate the last sentence (meaning the last set of strings that start with a capital letter) from the rest of the strings in a list. How’s that possible?

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 :

You can use the regex [A-Z][^A-Z]*$ to fetch that.
Example:

import re
s='Test. Test1. Test test test.'
last_sentence=re.findall('[A-Z][^A-Z]*$',s)
print(last_sentence)

Output:

['Test test test.']
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