1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
melomori [17]
3 years ago
8

#Write a function called 'string_type' which accepts one #string argument and determines what type of string it is. # # - If the

string is empty, return "empty". # - If the string is a single character, return "character". # - If the string represents a single word, return "word". # The string is a single word if it has no spaces. # - If the string is a whole sentence, return "sentence". # The string is a sentence if it contains spaces, but # at most one period. # - If the string is a paragraph, return "paragraph". The # string is a paragraph if it contains both spaces and # multiple periods (we won't worry about other # punctuation marks). # - If the string is multiple paragraphs, return "page". # The string is a paragraph if it contains any newline # characters ("\n"). # #Hint: think carefully about what order you should check #these conditions in. # #Hint 2: remember, there exists a count() method that #counts the number of times a string appears in another #string. For example, "blah blah blah".count("blah") #would return 3.

Computers and Technology
1 answer:
deff fn [24]3 years ago
8 0

Answer:

I am writing a Python program:

def string_type(string):

   if string=="":  //if the string is empty

       return "empty"

   elif string.count(".")>1:  #if the period sign occurs more than once in string

       if string.count("\n"):  #checks if the new line occurs in the string

           return "page"  #if both the above cases are true then its a page

       return "paragraph"  # if the period sign condition is true then its a para

   elif string.count(" ")>=1:  #if no of spaces in string occur more than once

       return "sentence"  #returns sentence

   elif len(string)==1:  # if length of the string is 1 this

       return "character"  #returns character

   else:  #if none of the above conditions is true then its a word

       return "word" #returns word

Explanation:

def string_type(string):  this is the definition of method string_type which takes a string as argument and determines whether the type of string is a word, paragraph, page, sentence or empty.

if string=="" this if condition checks if the string is empty. If this condition is true then the method returns "empty"

elif string.count(".")>1  This condition checks if the string type is a paragragh

string.count(".")>1   and if string.count("\n") both statements check if the string type is a page.

Here the count() method is used which is used to return the number of times a specified string or character appears in the given string.

Suppose the string is "Paragraphs need to have multiple sentences. It's true.\n However, two is enough. Yes, two sentences can make a paragraph."

The if condition first checks if count(".")>1 which means it counts the occurrence of period i.e. "." in the string. If the period occurs more than once this means it could be a page. But it could also be a paragraph so in order to determine the correct string type another if statement if string.count("\n") inside elif statement determines if the string is a page or not. This statement checks the number of times a new line appears in the string. So this distinguishes the string type paragraph from string type page.

elif string.count(" ")>=1: statement determines if the string is a sentence. For example if the string is "i love to eat apples." count() method counts the number of times " " space appears in the string. If the space appears more than once this means this cannot be a single word or a character and it has more than one words. So this means its a sentence.

  elif len(string)==1:  this else if condition checks the length of the string. If the length of the string is 1 this means the string only has a single character. Suppose string is "!" Then the len (string) = 1 as it only contains exclamation mark character. So the method returns "character" . If none of the above if and elif conditions evaluates to true then this means the string type is a word.

You might be interested in
What is the name of the the world cell of the internet?​
kipiarov [429]
<h2>Mobile Web</h2>

How you even don't know the basic information

it's disgusting.

3 0
3 years ago
Which of the following is NOT a computer peripheral?
Marianna [84]

Answer:CPU stands for the central processing unit. CPU is not a peripheral device.

Explanation:

CPU stands for the central processing unit. CPU is not a peripheral device.

7 0
1 year ago
Which of the following is a benifit of googling yourself ?
finlep [7]

Answer:

you are protecting yourself from identity theft

6 0
2 years ago
Marta, an art director, wants to give feedback to her graphic designer on his logo design. To be effective her feedback should b
Lana71 [14]

Answer:

B. Specific and provided during the construction of the project, and the end of the project.

Explanation:

The feedback have to be specific and provided during the construction of the project, and at the end of the project.

8 0
3 years ago
Read 2 more answers
What is software? Why is it needed? ​
sesenic [268]

Computer software, also known simply as software, also known by overseas Vietnamese as software is a collection of data or commands that instruct the computer to tell the computer how to work.  This is in contrast to the physical hardware, from which the system is built and actually does the work.  Because it helps people perform functions that humans can't do or do for a very long time

8 0
3 years ago
Other questions:
  • In what year did commercial use of the Internet become available? 1991 1996 1999 2001
    9·1 answer
  • Which tab would you click to access the autosum feature?
    9·1 answer
  • The house had a wonderful pool of ... (his/its/our) own.​
    5·1 answer
  • [Submit on zyLabs] Please write a function with one input, a matrix, and one output, a matrix of the same size. The output matri
    10·1 answer
  • Approximately what percent of desktop PCs are used for work-related purposes?
    13·2 answers
  • A _____________ is a method of controlled entry into a facility and provides access to secure areas such as a research lab or da
    6·1 answer
  • ___________ is a task pane used to correct grammar errors; opens when you click the Spelling &amp; Grammar button in the Proofin
    15·2 answers
  • to minimize wrist injury switch frequently among touch gestures the keyboard and the mouse true or false
    15·1 answer
  • After a business transaction has been analyzed, which of the following must occur for the analysis to have been done correctly?
    9·1 answer
  • 8. Assuming str(rate) is "7.25," what output would be produced by the following snippet:
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!