Vowels और Consonants Detect करना
पहले आपको Check करना चाहिए कि Character Alphabet है या नहीं। फिर आप Vowels की List बना सकते हैं और Check कर सकते हैं कि Character Vowel है या नहीं। यदि नहीं तो यह Consonant होना चाहिए।
उदाहरण
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))
Output:
h Consonant e Vowel l Consonant l Consonant o Vowel Neither p Consonant e Vowel o Vowel p Consonant l Consonant e Vowel
Improved Version (Unicode Support के साथ):
import unicodedata
def classify_char(c):
if not c.isalpha():
return 'Neither'
# Normalize और Lowercase
c_lower = c.lower()
# Basic Latin Vowels
vowels = 'aeiou'
# Extended Vowels (Accented Characters के लिए)
# Optional: Unicode Category Check
if c_lower in vowels:
return 'Vowel'
# Check Unicode Category for Letter Type
cat = unicodedata.category(c)
if cat.startswith('L'): # Letter
return 'Consonant'
return 'Neither'
One-liner Version:
vowels = set('aeiouAEIOU')
result = ['Vowel' if c in vowels else 'Consonant' if c.isalpha() else 'Neither' for c in "hello people"]