Can anyone help me with strings in Python?

How do you count the amount of words in this string using Python?
“HeLlO wOrLd!!11”, “Did you hear about the one-eyed one-horned Flying Purple People Eater?”, “12345”

Have you tried the .split() method?

what approaches have you considered? First think in steps, before expressing this in code.

1 Like

I did but I got an error an I cant seem to figure out the issue with my code due to the fact that i am new to programming.

Words = str("HeLlO wOrLd!!11", "Did you hear about the one-eyed one-horned Flying Purple People Eater?", "12345")
print("Number of words")
word = Words.split()
number_of_words = len(word)

This is my current code that I was attempting.

what do you attempt to do on the first line:

Words = str("HeLlO wOrLd!!11", "Did you hear about the one-eyed one-horned Flying Purple People Eater?", "12345")

Are you sure that is how to use the str() constructor? They are already strings. What you have there is a sequence, which if written as a list (or tuple) will permit iterating over each string, splitting it, and counting the elements in the result.

I was attempting to change entire line of code into a string so that python wouldnt have issues when counting the amount of words in the line.

Whenever you use a function (in this case, str) you have to consider what arguments that function accepts. Throwing a bunch of values at it and hoping it’ll do what you want… not so great. (pretty much any time you use a function you’ll want to read its documentation so that you can leverage the function in a reasonable way - or have some other way of reasoning about how it’ll behave that is strong enough)
It might, as a beginner, seem like you ideally shouldn’t have to read documentation, but, it’s the opposite. You should. That’s what programmers do. All. The. Time.

If you’ve decided what action you want to carry out (this is obviously a good thing, deciding what should happen before writing code) but don’t know how the language facilitates that action - go google it. Really, that’s probably exactly how you should be programming. Decide on actions, google the actions one after the other to see how they’re done.

If the result of googling is something you don’t understand then you should stop and look up the functions in it to see how they behave and how it all connects together. Maybe you’d even rewrite what you found when googling now that you see how the parts connect.

In any case, what difference does it make if you write several string literals and use some code to concatenate them, or if you write it as a single string literal from the start?

Also it seems a little bit strange to have separate words, then join them, then figure out how many separate words there were. Just don’t join them and write out how many there are? Maybe the input is really something else, some block of text.

Usually when I write a small program that operates on text, I’ll read everything from stdin (and if the information is in a file then that file can be directed to the program’s stdin):

→ cat 
       │ File:
   1   │ import sys
   2   │ 
   3   │ indata =
   4   │ print('input was:')
   5   │ print(repr(indata))
→ cat bananas 
       │ File: bananas
   1   │ lalal
   2   │ blah
→ python < bananas
input was:

You could also read smaller amounts at a time, even treat it like a stream of characters to operate on, but it probably doesn’t matter because you probably don’t have gigabytes of text anyway.

So after that you would have to ask yourself what “word” means. If it’s space-delimited, then count the space characters and add 1, or maybe there may be multiple spaces, then split on space and filter out empty and count the number of items. Maybe numbers aren’t words. More filtering, then count. …