I want to split string where the spaces are, and put it into an array. For example, if the str is “foo bar asdf” I want the array to be [“foo”, “bar”, “asdf”]. I know you can easily do that like so:
str = raw_input("Enter String")
cstr = ""
for char in str:
if char == " ":
print cstr
else:
cstr = cstr + char
But that outputs only the first word until the space, and it is quite bulky for something so simple. How can I do this simply?
This is what the
splitmethod on strings is for:The argument is the string to split on, or you can just do
.split()without an argument to split on any whitespace.