Question

Please help me with this python3 assignment. Write an iterative function, count_consonants(), that accepts a string...

Please help me with this python3 assignment.

Write an iterative function, count_consonants(), that accepts a string (that can include upper and lowercase characters) and returns the number of consonants in that string. A consonant is defined as any letter in the English alphabet other than the vowels a, e, i, o, u.

For example, if you call the function as follows:

count_consonants('correct horse battery staple')

the function should return 20.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def count_consonants(s):
    count = 0
    for ch in s:
        if ch.lower() not in "aeiou":
            count += 1
    return count


print(count_consonants('correct horse battery staple'))

Add a comment
Know the answer?
Add Answer to:
Please help me with this python3 assignment. Write an iterative function, count_consonants(), that accepts a string...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels...

    ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels in the string B) Count the number of consonants in the string C) Convert the string to uppercase D) Convert the string to lowercase E) Display the current string X) Exit the program The program should start with a user prompt to enter a string, and let them type it in. Then the menu would be displayed. User may enter option in small case...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • 10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string...

    10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string object arguments. Let's call them string1, string2, and string3. It should search string for all occurrences of string2. When it finds an occurrence of Programming Challenges string2, it should replace it with string. For example, suppose the three arguments have the following values: stringt: "the dog jumped over the fence" string 2 "the" string3: "that" With these three arguments, the function would return a...

  • Write a javascript function that accepts 3 arrays and returns a string of the elements sorted...

    Write a javascript function that accepts 3 arrays and returns a string of the elements sorted in ascending value, with duplicates removed. Return all alphabetic characters in lowercase. E.G. function([ 1,2,3, d], [5, 3, 0, ‘a’], [‘a’,’d’,9]) returns “0,1,2,3,5,9,a,d” E.G. function(['w', 'i', 'H', 5], [8, 5, 9, 'c', 'Z'], [6, 4, 'a', 'b', 'y']) returns “4,5,6,8,9,a,b,c,h,i,w,y,z” we must not use ' sort, remove, split,join and any built-in fucntions

  • Python3 : Write the function longestRun(s, chars) that takes a possibly-empty string s and a second...

    Python3 : Write the function longestRun(s, chars) that takes a possibly-empty string s and a second possibly-empty string of chars. We will say that a character is "good" if it is in the chars string (case insensitively, so "A" and "a" would match). The function should return the length of the longest consecutive run of good characters in the given string s. For example, consider: longestRun("abbcazBbcababb","bz"). This returns 3 (look for "zBb"). Restrictions: for loop, slicing cannot be used, if...

  • C++ Write a function named “hasDescendingDigits” that accepts a string of numbers. It returns true if...

    C++ Write a function named “hasDescendingDigits” that accepts a string of numbers. It returns true if the string contains the digits in descending order. The function can ignore (e.g. skip checking) all the characters that are not digits in the string. Empty string will return false. For example, string of “95421” or “862” or “8622” or “88” or “9” will return true and string of “95423” or “889” or “9445” or “449” or “” will return false.

  • using basic c++ and use no functions from c string library b) Write a fragment of...

    using basic c++ and use no functions from c string library b) Write a fragment of code (with declarations) that reads a pair of words from the keyboard and determines how many letters in corresponding positions of each word are the same. The words consist only of the letters of the English alphabet (uppercase and lowercase). For example, if the words are coat and CATTLE, the following output should be generated: 012245 0122 matching letter in position 0 t is...

  • Write a function named vertical that accepts a string as its parameter and prints each letter...

    Write a function named vertical that accepts a string as its parameter and prints each letter of the string on separate lines. For example, a call of vertical("hey now") should produce the following output: h e y n o w

  • Python please Write a function print_back_upper() that accepts a string my_str as an argument and displays...

    Python please Write a function print_back_upper() that accepts a string my_str as an argument and displays the string backwards and in uppercase, all in one line (Note: Create any necessary variables) For example: if my_str = "Python" then, your output should display as follows: NOHTYP (Hint: You can use for loop navigating up to range of len of string-1, -1, -1 and use reverse() and upper() functions to create and display the string backwards in uppercase)

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT