-1

I was reading my textbook, and I am confused what the importance of vowels_in_word = "" does? I'm not sure what the significance of it is. Does it just hold the the values of char? Also, when creating for loops, what is char? Can you just assign your own variable even if its not assigned anywhere in the program?

def get_vowels_in_word(word):
    vowel_str = "aeiouy"
    vowels_in_word = ""
    for char in word:
        if char in vowel_str:
            vowels_in_word += char
    return vowels_in_word
Dan Loewenherz
  • 10,879
  • 7
  • 50
  • 81
Veyronvenom1200
  • 95
  • 1
  • 1
  • 9
  • It means vowels_in_word variable is initialized to be empty. The code is finding the vowels in the word – Illusionist Feb 09 '17 at 19:37
  • The for loop iterates over the word one character (char) at a time – Illusionist Feb 09 '17 at 19:38
  • Possible duplicate of [Can't Understand For Loops Python](http://stackoverflow.com/questions/19257862/cant-understand-for-loops-python) – juanpa.arrivillaga Feb 09 '17 at 19:46
  • "Can you just assign your own variable even if its not assigned anywhere in the program?" I can't understand the question. Of course you can assign to variables. If something is assigned multiple times, then one of those assignments is first; and the first time it happens, it didn't happen before (that's what "first" means). Notice how you **don't** have the same question about `vowel_str = "aeiouy"`? **Why not**? – Karl Knechtel Jan 25 '23 at 07:08

1 Answers1

1

It initializes the string vowels_in_word to be empty. The following for loop then appends characters to it with the += operator.

The += operation would throw an exception if vowels_in_word had not previously been initialized to be a string of 0 characters.

heyiamt
  • 201
  • 2
  • 6
  • Could i name char something else, or is that built into python? Sorry if this is a bad question, Im trying to teach python to myself – Veyronvenom1200 Feb 09 '17 at 19:40
  • 1
    @user7433120 You can name it whatever you want. You should really be reading a tutorial, this isn't an appropriate question for stack-overflow, especially if you aren't showing any research effort. Don't use this site as a tutoring service. – juanpa.arrivillaga Feb 09 '17 at 19:42