Answer to Question #214149 in Python for Okasha

Question #214149

Sentence Capitalizer

Write a program with a function that accepts a string as an argument and returns a copy of the string with the first character of each sentence capitalized. For instance, if the argument is “hello. my name is Joe. what is your name?” the function should return the string “Hello. My name is Joe. What is your name?” The program should let the user enter a string and then pass it to the function. The modified string should be displayed.


1
Expert's answer
2021-07-06T00:32:58-0400
#a function that accepts a string as an argument and returns a copy of
#the string with the first character of each sentence capitalized.
#For instance, if the argument is "hello. my name is Joe. what is your name?"
#the function should return the string "Hello. My name is Joe. What is your name?"
def capitalizeSentence(sentence):
    sentenceLetters=[]
    sentenceLetters[:0]=sentence
    sentenceLetters[0]=sentenceLetters[0].upper()
    stopLetters = ('...', '.', '?', '!', '!!!')
    for index in range(1,len(sentenceLetters)-2):
        if sentenceLetters[index] in stopLetters and sentenceLetters[index+1]==' ':
            sentenceLetters[index+2]=str(sentenceLetters[index+2]).upper()
    return "".join(sentenceLetters)
        
#The program should let the user enter a string and then pass it to the function.
#The modified string should be displayed.
print(capitalizeSentence(input()))




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS