How can I replace the vowels of a word with underscores in python?
<p>I'm a beginner learning the python language and I'm stumped on how take the vowels of a word and replacing them with an underscore. </p>

<p>So far this is what I have come up with and it just doesn't work</p>

<pre><code>word = input("Enter a word: ")
new_word = ""
vowels = "aeiouy"


for letter in word:
    if letter != vowels:
        new_word += word
    else:
       new_word += "_"
       print(new_word)
</code></pre>

<p>To answer why your approach didn't work.</p>

<pre><code>if letter != vowels:
</code></pre>

<p>Does not do what you are thinking. It actually compares the letter against a full string <code>"aeiouy"</code>. It will always be unequal (e.g. <code>"a" != "aeiouy"</code> is True and so is any other letter).</p>

<p>More likely what you mean was</p>

<pre><code>if letter in vowels:
</code></pre>

<p>Which under the hood will iterate over <code>vowels</code> and compare each character to <code>letter</code>, returning <code>True</code> if any of the letters match.</p>

<p>The second mistake is here</p>

<pre><code>new_word += word
</code></pre>

<p>You are adding the original word to the new word rather than the letter you just checked. So make that</p>

<pre><code>new_word += letter
</code></pre>

<p>The third thing to note is that your logic is the reverse of what you intended. You wanted to replace vowels with <code>_</code> but your if statement allows vowels into the new word and replaces consonants with the underscore. So if you reverse your if and else clauses.</p>

<p>All up you end up with</p>

<pre><code>word = input("Enter a word: ")
new_word = ""
vowels = "aeiouy"

for letter in word:
    if letter in vowels:
        new_word += '_'
    else:
       new_word += letter

print(new_word)
</code></pre>

python