Remove Vowels from a String

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution:
    def removeVowels(self, s: str) -> str:
        vowels = "aeiou"
        result = []  # Initialize an empty list to store the non-vowel characters

        # Iterate through each character in the given string
        for char in s:
            if char not in vowels:  # If the character is not a vowel
                result.append(char) # Append it to the result list

        # Convert the result list into a string and return it
        new_string = ''.join(result)
        return new_string

Explanation

  1. We start with an empty list result that will hold all the non-vowel characters from the string s.
  2. Next, we use a for loop to iterate through each character in s.
  3. Inside the loop, we check if the current character is not in the vowels string.
  4. If it is not a vowel, we append it to the result list.
  5. Finally, we use the join method to convert the result list into a string, which we return.

This version of the code maintains the same functionality but uses multiple steps to provide more clarity into the process.