Page Contents
In this tutorial we will write a Python program to print all consonants in a string using list comprehension .
Steps to print all consonants in a string using list comprehension
- First we will declare a list of vowel .
- Then we will write a list comprehension and accept only that elements from the string in list which is consonant not vowel .
- Then we will iterate the list and print all the element .
Python program to print all consonants in a string using list comprehension
# Python program to print all consonants
# in a string using list comprehension
def print_consonant(mystring):
vowels=["a","e","i","o","u"]
# creating a list using list comprehension
mylist = [ item for item in mystring if item not in vowels]
for item in mylist:
print(item)
print_consonant("Rajkumar")
R j k m r
- In the above code first we have written a function named
print_consonant
. - Then we have created a list using list comprehension which only take consonant character from a string .
- After that we have iterated the list and print all the elements from the list
- After that we have called the function with a string . From the output we can see that all consonant characters has been printed .
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .