0

So the code below properly removes all the vowels from a string as expected.

def disemvowel(string):
    # Letters to remove & the new, vowerl free string
    vowels_L = list('aeiouAEIOU')
    new_string = ""

    # Go through each word in the string
    for word in string:

        # Go through each character in the word
        for character in word:

            # Skip over vowels, include everything elses
            if character in vowels_L:
                pass
            else:
                new_string += character

    # Put a space after every word
    new_string += ' '

    # Exclude space place at end of string
    return new_string[:-1]

no_vowels = disemvowel('Nasty Comment: Stack exchange sucks!')
print(no_vowels)

>>>>python remove_vowels.py
>>>>Nsty Cmmnt: Stck xchng scks!

However, when I move the statement: "new_string+= ' '" to where I think it should be (I come from a C/C++ background), I end up getting a weird answer,

def disemvowel(string):
    # Letters to remove & the new, vowerl free string
    vowels_L = list('aeiouAEIOU')
    new_string = ""

    # Go through each word in the string
    for word in string:

        # Go through each character in the word
        for character in word:

            # Skip over vowels, include everything elses
            if character in vowels_L:
                pass
            else:
                new_string += character

    # THIS IS THE LINE OF CODE THAT WAS MOVED        
    # Put a space after every word
    new_string += ' '

    # Exclude space place at end of string
    return new_string[:-1]

no_vowels = disemvowel('Nasty Comment: Stack exchange sucks!')
print(no_vowels)

>>>>python remove_vowels.py
>>>>N  s t y   C  m m  n t :   S t  c k    x c h  n g    s  c k s !

Instead of placing a space after a word has finished being iterated over exclusively, a space is also place wherever there was a vowel. I was hoping someone would be able to explain why this occurs, even though in C the result would be quite different. Also, any suggestions to streamline/condense the could would be welcome! : )

4
  • 4
    Your code isn't indented properly here, which makes it impossible to analyze without making guesses about how it's actually indented. Commented Apr 11, 2015 at 22:07
  • Sorry, oversight on my part. I fixed the indentation. Commented Apr 11, 2015 at 22:25
  • @martineau: Please don't fix problems in the original question. It makes nonsense of the solutions that explain how to correct the code Commented Apr 11, 2015 at 22:31
  • @Borodin: All I did was edit the question title. Commented Apr 11, 2015 at 23:10

2 Answers 2

8

for word in string doesn't iterate over the words; it iterates over the characters. You don't need to add spaces at all, because the spaces in the original string are preserved.

Sign up to request clarification or add additional context in comments.

6 Comments

Oh, that makes a total sense! Thanks for the explanation.
There is much more wrong with the OP's code than that!
@Borodin: There is, but it looks like mostly awkward ways of doing things or formatting errors introduced when the code was copied into the question. The indentation problems look exactly like what you'd get if you hit the code button and then pasted your code where the message tells you to put it.
@user2357112: Maybe, but I can't reproduce what you describe. Do you mean the Code snippet button, Ctrl/M? That's for JavaScript and makes something completely different. Or the Code sample button, Ctrl/K? That adds an extra four-space indent to the first line, but otherwise seems okay. Can you explain please?
@Borodin: The one with the braces. If you hit that button and then paste in code, only the first line gets the extra 4 spaces it needs, and everything after that comes out insufficiently indented. It's a long-standing problem that confuses new users again and again, and it's particularly annoying for Python questions, where indentation is crucial.
|
0

As interjay comments, your indentation is way out. Python relies on the indentation to describe which statements belong to what block, instead of the more common BEGIN ... END or { ... }.

In addition, user2357112 observes that you are expecting words from your string, whereas a string is simply a list of characters, and for word in string will set word to one character of string at a time

It is also much cleaner to use not in rather than an if together with a pass.

This is much closer to what you intended

def disemvowel(string):

    # Letters to remove & the new, vowel-free string
    vowels_list = 'aeiouAEIOU'
    new_string = ""

    # Go through each character in the string
    for character in string:

        # Skip over vowels, include everything else
        if character not in vowels_list:
            new_string += character

    return new_string


print disemvowel('Nasty Comment: Stack exchange sucks!')

output

Nsty Cmmnt: Stck xchng scks!

1 Comment

Yeah, I don't know why I thought typing 'word' would magically make python appropriately parse the string for me. Thanks for the tip about "not in", and refining the code I posted; pointing out the differences between good and bad code definitely helps me learn the language more quickly, so thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.