› Forums › Understanding Methods in Python: Why Does “” “.join(words)” Work?
- This topic is empty.
-
AuthorPosts
-
June 12, 2026 at 12:19 pm #6876
Understanding Methods in Python: Why Does “” “.join(words)” Work
While learning Python, I came across an interesting question:
Why do we write:
” “.join(words)
instead of:
words.join(” “)
To answer this, we first need to understand what a method is.
What Is a Method?
A method is a function that belongs to an object.
When we write:
text.split()
we are calling the “split()” method on the object “text”.
The general pattern is:
object.method(arguments)
Examples:
name.upper()
text.split()
numbers.append(5)In each case, the object before the dot owns the method.
Understanding “split()”
Suppose we have:
text = “I am a man”
When we call:
text.split()
Python breaks the string into a list of words:
[‘I’, ‘am’, ‘a’, ‘man’]
Notice that the string being split appears before the dot.
Understanding “join()”
Suppose we have:
words = [‘I’, ‘am’, ‘a’, ‘man’]
To combine these words into a sentence, we write:
” “.join(words)
Output:
I am a man
The string before the dot (“” “”) is the separator.
Python inserts that separator between each element of the iterable.
For example:
“-“.join(words)
produces:
I-am-a-man
and
“***”.join(words)
produces:
Iama***man
Why Doesn’t Python Use “words.join(” “)”?
This is a design choice.
Python’s designers decided that the separator should own the “join()” method.
Conceptually:
” “.join(words)
means:
«”Use this space string to join all the words together.”»
Many beginners expect:
words.join(” “)
because it reads like:
«”Words, join yourselves using a space.”»
That design would also be possible, but Python chose the opposite approach.
Can We Design It Differently?
Yes!
When creating our own classes, we can design methods however we want.
Example:
class WordList:
def init(self, words):
self.words = wordsdef join(self, separator):
return separator.join(self.words)words = WordList([“I”, “am”, “a”, “man”])
print(words.join(” “))
Output:
I am a man
Here the iterable appears before the dot and the separator becomes the argument.
This shows that method design is a choice made by the programmer.
A Useful Mental Model
Ask yourself:
«Which object knows how to perform the action?»
Examples:
text.split()
The string knows how to split itself.
name.upper()
The string knows how to convert itself to uppercase.
numbers.append(5)
The list knows how to add a new element.
” “.join(words)
The separator string knows what should be inserted between the words.
Key Takeaways
- Methods are called using the syntax:
object.method(arguments)
- The object before the dot owns the method.
- “split()” is a method of strings that converts a string into a list.
- “join()” is also a method of strings, but it is called on the separator string.
- Method design is a choice made by the class designer.
- When creating our own classes, we can choose different method interfaces if they make sense.
A good way to remember:
sentence.split() # String → List
” “.join(words) # List of strings → String“split()” breaks things apart, while “join()” glues them back together.
-
AuthorPosts
- You must be logged in to reply to this topic.
