Python.
create a function that takes a string as a parameter and prints out the following details as the example output below. If the string is "Hello, my name is Starling, Clarice. My, my, my, nice to meet you." The output would be:
Unique words: 10
Number of commas: 5
Number of periods: 2
Number of sentences: 2
(HINT: Use the following string methods: split, lower, count, replace, format, and use the following functions: set, len.
You may need to use some methods and functions more than once.)
Python code:
#defining function
def analyze_sentence(sentence):
#storing count of "," to c
c=sentence.count(",")
#storing count of "," to p
p=sentence.count(".")
#storing number of sentences to ns
ns=p
#replacing "," with ""
sentence=sentence.replace(",","")
#replacing "." with ""
sentence=sentence.replace(".","")
#converting sentence to lowercase
sentence=sentence.lower()
#storing number of unique words to u
u=len(set(sentence.split()))
#printing unique word count
print("Unique words: {}".format(u))
#printing comma count
print("Number of commas: {}".format(c))
#printing period count
print("Number of periods: {}".format(p))
#printing sentence count
print("Number of sentences {}".format(ns))
#calling function analyze_sentence
analyze_sentence("Hello, my name is Starling, Clarice. My, my, my, nice to meet you.")
Screenshot:
Output:
Get Answers For Free
Most questions answered within 1 hours.