Ex 41 - why is the str() there?

Hey there :slight_smile:
I have a random question. In the code for ex41 there’s this on line 31:

WORDS.append(str(word.strip(), encoding="utf-8"))

I was just wondering why we had to put the str() there first?
Without it it gives me an error:

TypeError: append() takes no keyword arguments

But there are no keyword arguments in the file where we take all the words, so I’m a little confused…

Thanks for reading :slight_smile:

That error means you did the edit wrong. Look at the parenthesis:

WORDS.append(
   str(
      word.strip(), encoding="utf-8"
      )
)

See how the , encoding="utf-8" is part of the str() call and not the append call?

I still don’t really understand…
I took out the str and the relevant parenthesis and still got that error.

I just don’t understand why we need the str(), or more precisely, why when we open the file do we have to convert the contents to a string? Is it not already a string? Is it a general rule that downloaded files are always binary encoded (and therefore need to be decoded?)

And if so why is that the error? Binary encoded words aren’t keyword arguments.

I know this is quite nitpicky, I just don’t want a future error to come up and I’ll be utterly stuck…

Thank you

So your new code looks like this?

WORDS.append(str(word.strip())

If that’s true then you should NOT get that error that says:

TypeError: append() takes no keyword arguments

This error is saying you did this:

WORDS.append(word.strip(), encoding="utf-8")

If you break this down you can see the problem. Let’s turn everything into variables:

stripped_word = word.strip()
utf8_encoded_word = str(stripped_word, encoding="utf-8")
WORDS.append(utf8_encoded_word)

Now, that’s the correct code but “exploded out” into multiple variables so you can see each step. Any time you can’t understand a chain like this turn it into a bunch of variables to see each step. Next, if you’re still getting that error then you probably did this:

stripped_word = word.strip()
utf8_encoded_word = str(stripped_word)
WORDS.append(utf8_encoded_word, encoding="utf-8")

See how there’s the encoding="utf-8" in the append call? But in my code right above this that encoding="utf-8" is in the str call where I make the variable utf8_encoded_word.

That’s the first thing to figure out in your confusion so, paste me back your snippet of code where you get this error.

The reason I’m hammering on this confusion is you think I just did this:

x = str(y)

But I didn’t, I did:

x = str(y, encoding="utf-8")

I think your confusion is because you think the encoding="utf-8" belongs to the append and not the str. That’s why you see it doing nothing and also why you get that error.

The reason I did str(y, encoding="utf-8") is probably because I kept running into people with computers that are configured for non-utf8 characters, like Big5. In those cases their computer will save text files in Big5, and then Python throws a little fit because it wants everything to be utf-8. Your computer probably is set to either utf-8 or an English language character set so you don’t need it.

But, the most important thing is for you to figure out that error after you change it.

Ahh I see my mistake, I didn’t change the code properly, now it doesn’t give me that error at all. That cleared it up for me. Also looked over the str parameters and saw it needs a specified encoding just in case.

Thank you!!

A free service run by Zed A. Shaw for learncodethehardway.org.