0

I'm trying to understand how functions work, referencing the example below. I understand that (stuff) is an argument passed into the function when called.

My question is about (' ') on line 4. It's formatted like an argument, and when I run the function these single quotes encapsulate each word. This has me befuddled, is it an argument? How does Python know to put each word inside these single quotes?

def break_words(stuff):
    """This function will break up words for us."""
    words = stuff.split(' ')
    return words
2
  • 1
    first read python doc about split(). Commented Jul 9, 2014 at 20:55
  • split(' ') is running the split function with the argument ' ' which is a string, containing a single space. The return value of break_works will be something like ['word', 'word2', 'word3'] where each of the words is a string, by itself. Commented Jul 9, 2014 at 20:58

6 Answers 6

1

It is an argument: a string containing a single space. It's just telling Python to split the stuff variable, which is also a string, by spaces.

Sign up to request clarification or add additional context in comments.

1 Comment

This makes sense, the single quotes themselves aren't returned, but indicate to the split function Where to split the string. If for example I were to have 'i' instead of ' ' as the argument. 'Th', 's funct', 'on w', 'll break up words for us' would be returned.
1

If you are confused about single quotes around strings, that is just how Python prints strings in the interpreter by default.

It is not that "Python" knows how to put the words into single quotes. The split function returns strings each containing a word, and the Python interpreter prints strings that way.

Comments

0

The split function will take a string and split it into multiple portions. Each of these portions becomes an item in a list, and so a list of all those portions is returned to you.

When split is called with one argument (a string or a char), it means that the function will split the string every time it encounters that string/char. Here, the argument is the space character, so the string is split at every space.

There's nothing about split that adds ' around anything. That happens automatically when you look at a representation of a string. It just means you've been returned a list of strings, and there are pairs of ' around each one. It's for the benefit of the programmer, to indicate that they're looking at a string object.

Comments

0

The single ' ' that you are seeing in the returned words variable is denoting the elements of the list are strings. If you change line 3 to have double quotes inside the () you will still see single quotes on the returned list.

Comments

0

Python does't encapsulate each word

Probably you print list returned by break_word like that

print break_word("Hello World") 

then Python use special function to print list and it uses quotas to shows you than you have text on list.

Do this - print list elements one by one:

for word in break_word("Hello World"):
    print word.

and you see text without quotas.

Comments

0

It is probably easier to understand if you see the function in action. Please note the changes I made in the code!

To use this function to break up words would look like this:

def break_words(stuff):
    """This function will break up words for us."""
    stuff = stuff.split(stuff=' ')
    return stuff
stuff = "This function will break up words for us."
print stuff.split()

Then, the printed result is this:

['This', 'function', 'will', 'break', 'up', 'words', 'for', 'us.']

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.