Why is it string.join(list) instead of list.join(string)?
The join() method is a string method, so it is called on a string object and takes a list of strings as its argument.
The join() method is a string method, so it is called on a string object and takes a list of strings as its argument. This makes sense when you consider that the join() method is used to create a single string from a list of strings by concatenating all the strings in the list, using the string on which the join() method is called as a separator.
For example, if you have a list of strings:
Define a simple list of strings in Python
words = ['cat', 'dog', 'fish']
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Python - The Practical Guide</div>
You can use the join() method to create a single string from the list, like this:
Create a single string from a list of strings in Python using the join method
words = ['cat', 'dog', 'fish']
sentence = ' '.join(words)
print(sentence)The resulting string sentence would be: "cat dog fish"
The join() method is often used to create a single string from a list of strings because it is a convenient and efficient way to do this. It is much faster than concatenating the strings manually using a for loop, for example.
I hope this helps! Let me know if you have any other questions.