SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
More About Strings
Exercise:
Programming Exercises
Question:8 | ISBN:9780132576376 | Edition: 2

Question

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.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Sentence Capitalizer Program Code:

def capitalizeSentences(string):
    # Split the string into sentences 
    #using period ('.') as the delimiter
    sentences = string.split('. ')

    # Capitalize the first character of each sentence
    capitalizeSentences = [sentence.capitalize() for sentence in sentences]

    # Join the capitalized sentences back into a single string
    modified_string = '. '.join(capitalizeSentences)

    return modified_string


# Prompt and read a string from the user
string = input("Enter a string: ")

# Call the capitalize_sentences function
modifiedString = capitalizeSentences(string)

# Display the modified string
print("Modified string:", modifiedString)

Executed Output 1:

Enter a string: hello. my name is Joe. what is your name?
Modified string: Hello. My name is joe. What is your name?

Executed Output 2:

Enter a string: how are you?
Modified string: How are you?

 

0 0

Discussions

Post the discussion to improve the above solution.