(1) Prompt the user to enter a string of their choosing. Output the string. (1 p
ID: 3906975 • Letter: #
Question
(1) Prompt the user to enter a string of their choosing. Output the string. (1 pt) Ex: Enter a sentence or phrase: The only thing we have to fear is fear itself. You entered: The only thing we have to fear is fear itself. (2) Complete the get_num_of_characters() function, which returns the number of characters in the user's string. We encourage you to use a for loop in this function. (2 pts) (3) Extend the program by calling the get_num_of_characters() function and then output the returned result. (1 pt) (4) Extend the program further by implementing the output_without_whitespace() function. output_without_whitespace() outputs the string's characters except for whitespace (spaces, tabs). Note: A tab is ' '. Call the output_without_whitespace() function in main(). (2 pts) Ex: Enter a sentence or phrase: The only thing we have to fear is fear itself. You entered: The only thing we have to fear is fear itself. Number of characters: 46 String with no whitespace: Theonlythingwehavetofearisfearitself. LAB ACTIVITY 16.18.1: Ch 6 Warm up: Text analyzer & modifier (Python 3) 4160 main.py Load default template... 1 inputStr = input('Enter a sentence or phrase: ') print('You entered:', inputStr) Hamina def get_num_of_characters(inputStr): count = 0 for i in range(0, len(inputStr)): count = count + 1 return count; print (" Number of characters:",get_num_of_characters (inputStr)) def output_without_whitespace (inputStr): strWithout Space = "" for i in range(0, len(inputStr)): if inputStr[i] != '': strWithout Space = strWithout Space + inputStr[i] return strWithout Space; 18 if 19 name="main ": print ("String with no whitespace:", output_without_whitespace(inputStr))Explanation / Answer
def get_num_of_characters(string): count = 0 for i in range(len(string)): count += 1 return count def output_without_whitespace(string): print('String with no whitespace: ', end='') for ch in string: if ch != ' ': print(ch, end='') print() sentence = input('Enter a sentence or phrase: ') print('You entered: ' + sentence) print(' Number of characters: ' + str(get_num_of_characters(sentence))) output_without_whitespace(sentence)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.