Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capitalize the first letter after a punctuation

Tags:

python

For example, I have this sentence:

hello. my name is Jess. what is your name?

and I want to change it into:

Hello. My name is Jess. What is your name?

I came up with this code, but I have one problem with connecting everything back together

def main():
    name = input('Enter your sentence: ')
    name = name.split('. ')
    for item in name:
        print (item[0].upper() + item[1:], end='. ')

When I put in the sentence, it will return:

Hello. My name is Jess. What is your name?.

How can I stop the punctuation from appearing at the end of the sentence? Also, what if I have a question in the middle, for example:

hi. what is your name? my name is Jess.
like image 285
hn_03 Avatar asked Sep 06 '25 07:09

hn_03


1 Answers

This one is better solution

x = "hello. my name is Jess. what is your name?"
print( '. '.join(map(lambda s: s.strip().capitalize(), x.split('.'))))

output:
Hello. My name is jess. What is your name?
like image 176
Ramesh K Avatar answered Sep 10 '25 00:09

Ramesh K