Python Program to find if a character is a vowel or a Consonant

Basics of vowel and Consonants

In the English language, there are 26 alphabets, out of which a, e, I, o, and u have considered vowels, and the rest have considered consonants.

In the article, we will use different methods to check whether the given character is a vowel or a consonant.

Agenda of the article:

  • If the input is any of a, e, i, o, u, A, E, I, O, or U, then we print the given character is a vowel.
  • Else, we print the given character is a consonant.

First Method - Using a List of Vowels

In this method, we will use a list containing all vowels and try to match the input with the elements of the list.

Output:

Enter a character: u
The character 'u' is a vowel!

Enter a character: E
The character 'E' is a vowel!

Enter a character: k
The character 'k' is a consonant!

In the above example, we have used the input function to get a character from the user. We have created a list containing vowels both in lower and upper case. Then, we have checked whether the given character is a vowel or not using 'in' operator, and we have printed the result based on the input.

Time Complexity = O(1) : This is because it takes a constant amount of time to execute, regardless of the input size.

Space Complexity = O(1) : The above program does not require any extra space.

Note that in Python, you can check if a value is in a list using the 'in' keyword.

Second Method - Extension of Method 1

In this method, we will create an isVowel function to implement the same idea as above.

Output:

Enter a character: i
The character 'i' is a vowel!

Enter a character: m
The character 'm' is a consonant!

In the above method, we have wrapped the whole idea within the isVowel function. The function first matches the passed character with the elements of the vowels list and prints the output based on the input character. Also, we can call the function as many times as we want.

Time Complexity = O(1): The time required by the program is constant and does not change with input.

Space Complexity = O(1): The above program also does not require any extra space.

Note: We can also use the comparison operator == to compare the character.
For example: if x == 'a' or x == 'e' or x == 'i' or x == 'o' or x == 'u'. But this method is not the best practice. We should always look to write a better code.

Third Method - Using a string of vowels

In this method, instead of a list, we have stored the vowels in a string named 'vowels' and used the find(character) method to implement the isVowel function.

Output:

Enter a character: a
The character 'a' is a vowel!

Enter a character: z
The character 'z' is a consonant!

In this program, the find() method is called on the vowels string to check if the input character is present in the vowels string or not. If the character is found in the vowels string, the find() method returns the index of the first occurrence of the character in the string. If char is not found, find() returns -1.

Note that the find() method has a time complexity of O(n), where n is the length of the searched string. In this case, the find() method is called on a string of fixed length (i.e., the vowels), so the time complexity of the function is O(1), which means it takes a constant amount of time to execute regardless of the input size.

Time Complexity = O(1) - The program requires no extra time.

Space Complexity = O(1) - Also, the program requires no extra space.

Fourth Method - Using Regular Expression

This method will use the match() method from the re module (re = regular expression).

The re.match() is a Python function provided by the built-in re (regular expression) module that attempts to match a pattern at the beginning of a string. Also, the re.match returns a match object.

The syntax of re.match() is as follows:

Code:

Output:

Enter a character: a
The character 'a' is a vowel!

Enter a character: x
The character 'x' is a consonant!

In the above program, re.match() function matches the given regular expression pattern (first argument) against the given string (second argument). In this case, the regular expression pattern is the character to be checked (char), and the string is the vowels string. If a match is found, re.match() returns a match object. Otherwise, it returns None.

If re.match() returns a match object, it means that the character is a vowel, and the function prints a message saying so. Otherwise, it means that the character is not a vowel (i.e., it is a consonant), and the function prints a message accordingly.

Note that the character to be checked (char) is the regular expression pattern's first argument, and the string to be matched against (vowels) is it's second. Contrary to regular expression convention, which calls for the string to be the first argument and the pattern to be the second, this method uses the string as the first argument.

Time Complexity = O(1) - The time complexity of the internal re.match() function is O(m), where m is the size of the regular expression pattern. The time complexity of re.match() is constant since the regular expression pattern has a fixed length of 10 (the length of the vowels string) in this case.

Space Complexity = O(1) - Due to the fact that the size of the regular expression pattern is fixed, the re.match() function doesn't require any extra memory.

It's important to consider the specific use case and requirements when choosing an approach for checking whether a character is a vowel or not. If efficiency is a major concern for large inputs, a simpler approach (mentioned above in the article) may be more appropriate. However, if flexibility and ease of modification are important, the re.match() approach may be better choice.

Method Five - Using a Dictionary and Vowels as Keys

In this method, we have used a dictionary of vowels where each key represents a vowel in upper or lower case and all the set to 'vowel'.

Output:

Enter a character: U
The character U is a vowel!

Enter a character: X
The character X is a consonant!

The function maps vowels to their corresponding values, which are all set to "vowel," by using a dictionary of vowels. Using the dict.get() method, the function returns the value related to the specified character in the dictionary vowels.

The word "vowel" is returned if the character can be found among the dictionary's vowels. The optional input "consonant" is returned as the default value if the character cannot be found in the dictionary. This indicates that the function will return "consonant" if the input character is not a vowel.

Time Complexity = O(1): Because the dict.get() method used in the isVowel() function performs a constant-time dictionary lookup operation.

Space Complexity = O(1): Because the vowels dictionary is fixed in size and does not depend on the input size.

Method Six: Using the ASCII value and Ord() Function

The ASCII equivalents of vowels are as follows:

CharacterDecimalHexa-DecimalBinary
A650x4101000001
E690x4501000101
I730x4901001001
O790x4F01001111
U850x5501010101
a970x6101100001
e1010x6501100101
i1050x6901101001
o1110x6F01101111
u1170x7501110101

In this method, we created a list containing the corresponding ASCII values of vowels in upper and lower case. The ord() function returns the corresponding ASCII value of the character in decimal. If the integer value is present in the list, the function will tell the character is a vowel. Else, the character is a consonant.

Output:

Enter a character: O
The character 'O' is a vowel!

Enter a character: G
The character 'G' is a consonant!

The function uses the ord() function to get the ASCII value of the input character char. Then checks if the ASCII value of char is present in the vowels list using the in operator.

If the ASCII value of char is found in the vowels list, the function prints that the character is a vowel. Otherwise, it prints that the character is a consonant.

Time Complexity = O(1): Because all the operations in the function, such as list indexing, checking for the presence of a value in a list, and ASCII value comparison, are constant time operations.

Space Complexity = O(1): Because the vowels list is fixed in size and does not depend on the input size. The input character and the output messages printed by the function take up constant space.

Method Seven: Using the countof() function of the Operator Module

In this method, we created a string of vowels and used the operator.countof() method that returns the count value of the passed character present in the passed string.

Output:

Enter a character: I
The character 'I' is a vowel!

Enter a character: V
The character 'V' is a consonant!

The function uses the countOf() function to count the number of occurrences of the input character char in the "vowels" string.

  • If the count is greater than 0, it means that the character is a vowel, and the function prints that "the character is a vowel".
  • Otherwise, if the count is 0, it means that the character is not present in the "vowels" string. Hence it is a consonant and the function prints that "the character is a consonant".

We hope you have found this article useful in enhancing your knowledge. We would like to hear from you about which method you found better than all. Please go through the future improvements for further enhancements in the programs.

FUTURE IMPROVEMENTS: What should you do?

The programs to check whether an input is a vowel or a character could use the following future improvements:

  1. Input Validation: Verify whether the input is a single character by doing input validation. Input validation tests should be included to ensure that the input is indeed a single character; if not, the user should be prompted for the correct input.
  2. Case Sensitivity: The present implementations differentiate between uppercase and lowercase vowels based on the case. You might change the input character to lowercase or uppercase before determining if it is a vowel if you want the program to be case-insensitive. In this method, vowels in both uppercase and lowercase would be recognized by the program as vowels.
  3. Error Handling: Check whether the input character is an alphabet or not. Instead of using an alphabet, the user might enter a special character or a digit. The program would be more reliable if error handling is added to handle such situations.
  4. Add Test Cases: It would be advantageous to provide test cases to confirm the program's accuracy. To completely test the program's functioning and manage any potential edge situations, you may add a variety of scenarios, including uppercase and lowercase vowels, consonants, special characters, and numerals.
  5. User-Friendly Output: Consider making the output messages more user-friendly by adding information about whether the input letter is a vowel or a concise and easy-to-understand consonant. The program would become more user-friendly and improve the user experience.

These improvements can make the program determining whether a given input is a vowel or a consonant more reliable, effective, and user-friendly.






Latest Courses