Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 24 Online
OpenStudy (anonymous):

I don't understand the syntax of string.split http://docs.python.org/library/string.html?highlight=string.split#string.split print string.split(grrrr wtf noWork) returns invalid syntax for 'wtf'. with '' around the string it returns nameError 'name "string" not defined'. I've tried different ways, but none works. what's 'erratic' about the input? and what's the correct syntax? thanks!

OpenStudy (anonymous):

what i want to do is, for pset4, 1. try a 'shift' 2. split the returned string into a list of words and 3. count the valid ones... i wanted to try the function with a simple example first, to see how it worked. but instead i got stuck:-)

OpenStudy (anonymous):

The split function takes a string, and turns it into a list of strings split by the character(s) that you give it. The character defaults to a space. One common mistake with string methods is that you're not putting the word 'string' before the dot, you're putting an actual string, or at least a variable containing a string. The thing you want to separate the strings goes in the parentheses. But that explanation is meaningless without some examples! "hi there".split() ====> ["hi","there"] "bananas".split("a") ====> ["b","n","n","s"] "1,2,3,4,5".split(",") ====> ["1","2","3","4","5"] (a good way to build a list quickly) "barbarian".split("ar") ====> ["b","b","ian"] "lots of space".split() ====> ["lots","of","space"] (get rid of that extra space!) All of these work if you assigned the string to, say, myString. myString = "Weitgeist" myString.split('e') ====> ["W","itg","ist"]

OpenStudy (anonymous):

overwhelming answer:-) but what do mean with "One common mistake with string methods is that you're not putting the word 'string' before the dot" --> isn't the point rather that i put the word 'string' there instead of the actual string?

OpenStudy (anonymous):

Yes, I said the opposite of what I mean. Let me try that again. A common mistake is that people see examples that look like string.split() and think they actually have to type "string" when really that's a placeholder for the actual string (or string variable) that you are using. Is that better? Sorry.

OpenStudy (anonymous):

Oh, and did you mean "overwhelming" in a bad way? Because I can try again. :)

OpenStudy (anonymous):

Oh, and I just noticed it's Weltgeist, not Weitgeist, which I used in my example. Whoops.

OpenStudy (anonymous):

haha, no i'm not that subtle... overwhelming as in simple question -- very elaborate answer:) (weltgeist, btw, is german for world spirit. Hegel used the term to designate the totality of history. as of 1820, the weltgeist, according to hegel, was residing in berlin, prussia. but he predicted it would soon be moving towards the west and the united states of america. it's fun to think about, i think.)

OpenStudy (anonymous):

and i really got the concept. its's a good point about writing, or i mean not writing 'string'.

OpenStudy (anonymous):

I think I read the name and was like "Oh...similar to Zeitgeist." As for my answers, I'm nothing if not thorough. :)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!