
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
How to detect vowels vs consonants in Python?
First you should check if the character is an alphabet or not. Then you can create a list of vowels and check if the character is a vowel using this. If not then it must be a consonant. For example,
def vowel_or_consonant(c): if not c.isalpha(): return 'Neither' vowels = 'aeiou' if c.lower() in vowels: return 'Vowel' else: return 'Consonant' for c in "hello people": print c, vowel_or_consonant(c)
This will give the output:
h Consonant e Vowel l Consonant l Consonant o Vowel Neither p Consonant e Vowel o Vowel p Consonant l Consonant e Vowel
- Related Articles
- Alternating Vowels and Consonants in C/C++
- Frequency of vowels and consonants in JavaScript
- Validating alternating vowels and consonants in JavaScript
- Moving vowels and consonants using JavaScript
- How to count number of vowels and consonants in a string in C Language?
- How to Count the Number of Vowels and Consonants in a Sentence in Golang?
- Program to sort all vowels at beginning then the consonants, are in sorted order in Python
- C# Program to count number of Vowels and Consonants in a string
- Check if a string can be converted to another string by replacing vowels and consonants in Python
- Arrange consonants and vowels nodes in a linked list in C++?
- Java Program to Count the Number of Vowels and Consonants in a Sentence
- Swift Program to Count the Number of Vowels and Consonants in a Sentence
- Haskell Program to Count the Number of Vowels and Consonants in a Sentence
- Kotlin Program to Count the Number of Vowels and Consonants in a Sentence
- C Program to count vowels, digits, spaces, consonants using the string concepts

Advertisements