Python Program to Print all Consonants in a String

Python Program to Print all Consonants in a String

In this article, we will be solving question based on Strings and Conditional Statements in Python. According to the question we have to print all the consonants present in a string.

If you like the article content, then share it in your coding groups so that more people can take advantage of the content. So, without wasting any time let's solve this question.

Question - Write a Python Program to Print all Consonants in a String

Python Program to Print all Consonants in a String


Logic and Explanation

Question - Write a Python Program to Print all Consonants in a String

Python Program to Print all Consonants in a String - In this program we have to print all the consonants in a string so we will be going to iterate through character by character and then we will be checking it for consonant. 

Now, there are too many consonants as compared to vowels so we will check the letter for vowel. If a letter is not vowel, then it is a consonant and we will print it.

So, let's start writing with code and the first task will be iterating through the string. For this we will be running a simple for loop as shown below. Now, one by one the characters from the string s will come in variable char and it's time to check for consonants. Before that we will check that whether the character is alphabet or not as string can contain digit or special characters and we are required to print only the consonants. So, for this we will be using the built-in Python string method isaplha(). 

s = "Hello World"

for char in s:


For checking we will be creating a tuple with all the vowels in lowercase. If the lowercase of char in loop is not present in that tuple, this means that it is a consonant and we will print it. I hope the logic and approach for the question is clear to you.

Here's the code for the same.

s = "Hello World"

for char in s:
    if char.isalpha():
        if char.lower() not in ('a','e','i','o','u'):
            print(char)


Output

H
l
l
W
r
l
d


So, I hope you liked the article and you would have found the content of the article useful and helpful for you. Share this article in your coding communities so that more people can take advantage of the content. Also, In case of any doubt feel free to ask in the comments below.

For More Such Python Programs - Click Here

Enjoy Coding!!!


Also Read :-

Extract Phone Number Details Using Python





For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment (0)

Previous Post Next Post