Question

Python. create a function that takes a string as a parameter and prints out the following...

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.)

Homework Answers

Answer #1

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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first,...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first, third, fifth (and so on) characters of the strings, with each character both preceded and followed by the ^ symbol, and with a newline appearing after the last ^ symbol. The function returns no value; its goal is to print its output, but not to return it. Q2) Write a Python function called lines_of_code that takes a Path object as a parameter, which is...
In PYTHON please: Write a function named word_stats that accepts as its parameter a string holding...
In PYTHON please: Write a function named word_stats that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report the total number of words (as an integer) and the average word length (as an un-rounded number). For example, suppose the file tobe.txt contains the following text: To be or not to be, that is the...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will implement Queue Abstract Data Type with the following functions/methods.  Any build-in/pre-defined Queue function/library (e.g., java.util.Queue in Java) is NOT allowed to use in your code. push(Element):  insert the input Element (e.g., String or Integer in Java) to the end of the queue. pop(): remove the head element of the queue and print the head element on screen. count():  return the total number of elements in the queue...
write a code in python Write the following functions below based on their comments. Note Pass...
write a code in python Write the following functions below based on their comments. Note Pass is a key word you can use to have a function the does not do anything. You are only allowed to use what was discussed in the lectures, labs and assignments, and there is no need to import any libraries. #!/usr/bin/python3 #(1 Mark) This function will take in a string of digits and check to see if all the digits in the string are...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the functions defined below. You are expected to re-use these functions in implementing other functions in the file. Include a triple-quoted string at the bottom displaying your output. Here is the starter outline for the homework: d. def count_words(text): """ Count the number of words in text """ return 0 e. def words_per_sentence(text): return 0.0 f. def word_count(text, punctuation=".,?;"): """ Return a dictionary of word:count...
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT