Home > Software design >  Lowercase the first letter of the first word of each sentence in a paragraph
Lowercase the first letter of the first word of each sentence in a paragraph

Time:11-06

How to lowercase the first letter of the first word of each sentence in a paragraph? Also, nouns in the middle of sentences will remain capitalized.

How can i do that in phyton?

For example: "This is a example sentence. Please help me. I don't want this situation. In Berlin we have a great time." to "this is a example sentence. please help me. i don't want this situation. in Berlin we have a great time."

enter image description here i tried this one but this is lower only one sentence.

CodePudding user response:

I would use a regex for this:

import re

s = "This is a example sentence. Please help me. I don't want this situation. In Berlin we have a great time."

out = re.sub(r'((?:^|\.)\s*\w )', lambda m: m.group().lower(), s)

output: "this is a example sentence. please help me. i don't want this situation. in Berlin we have a great time."

regex demo

CodePudding user response:

def first_lower(s):
    paragraph = ''
    _sentences = s.split('.') # split sentences from paragraph using .
    for sentence in _sentences:
        if sentence == '':
            continue
        paragraph  = sentence[0].lower()   sentence[1:]   '.' # append converted string to paragraph
    
    return paragraph

string = """this is a example sentence. please help me. i don't want this situation. in Berlin we have a great time."""
print(first_lower(string))

this is a example sentence. please help me. i don't want this situation. in Berlin we have a great time.

I hope this will answer your question.

  • Related