Write a python program to remove vowels in a word

1. Introduction

In this blog post, we will write a Python program to remove all vowels from a given word. This task is an excellent exercise in string manipulation and illustrates the use of loops, conditionals, and string methods in Python.

2. Program Steps

1. Define a string containing all vowels (both lowercase and uppercase).

2. Iterate over each character in the input word.

3. If the character is not a vowel, concatenate it to a new string.

4. Return or print the new string without vowels.

3. Code Program

def remove_vowels(word):
    # Step 1: Define a string of vowels
    vowels = 'aeiouAEIOU'

    # Initialize an empty string to store the result
    result = ''

    # Step 2: Iterate over each character in the word
    for char in word:
        # Step 3: Check if the character is not a vowel
        if char not in vowels:
            # Concatenate non-vowel characters to the result
            result += char

    # Step 4: Return the result
    return result

# Example usage
input_word = "HelloWorld"
output = remove_vowels(input_word)
print(f"Word after removing vowels: {output}")

Output:

For the input word 'HelloWorld', the output will be 'HllWrld'.

Explanation:

1. The function remove_vowels starts by defining a string vowels containing all the vowels.

2. It then initializes an empty string result which will hold the final word after vowel removal.

3. The function iterates over each character in word using a for loop. In each iteration, it checks if the character is not present in the vowels string.

4. If a character is not a vowel, it's concatenated to result. This is done using result += char.

5. Finally, the result string, which contains the original word minus its vowels, is returned and printed.

This program demonstrates an efficient way to remove vowels from a word in Python, showcasing fundamental string manipulation techniques.


Comments