87.8k views
4 votes
Write a Python 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 ____. What is your name?" The function should return the string:

User Jandi
by
8.5k points

1 Answer

4 votes

Final answer:

A Python program with a function 'capitalize_sentences' is provided, which capitalizes the first character of each sentence in a string and demonstrates its usage with an example.

Step-by-step explanation:

The subject of this question is Computers and Technology, specifically programming with Python. Below is a Python program that includes a function capitalize_sentences which takes a string as an argument and returns a copy with each sentence's first character capitalized.

def capitalize_sentences(text):
sentences = text.split('. ')
sentences = [s.strip().capitalize() for s in sentences if s]
return '. '.join(sentences)

# Example usage:
input_string = "hello. my name is ____. what is your name?"
capitalized_string = capitalize_sentences(input_string)
print(capitalized_string)

This program first splits the input string into a list of sentences, strips any leading or trailing whitespace, capitalizes the first character of each sentence, and then joins the sentences back into a string, properly capitalizing the first character of each sentence.

User Rohit Khandelwal
by
8.2k points