Why is it string.join(list) instead of list.join(string)?


In Python, the join() method is a string method that concatenates a list of strings with a separator string. The syntax for using the join() method is separator.join(list), where separator is the string that you want to use to join the elements of the list, and list is the list of strings that you want to join. For example, you could use the join() method like this:

words = ['one', 'two', 'three']
separator = '-'
sentence = separator.join(words)
print(sentence)
This would output the string 'one-two-three'.

On the other hand, the join() method is not available on lists in Python. Instead, you can use the join() method on a string to concatenate a list of strings. For example:

words = ['one', 'two', 'three']
separator = '-'
sentence = separator.join(words)
print(sentence)
This would also output the string 'one-two-three'. The reason that the join() method is a string method and not a list method is because the join() method operates on a string separator, and not on the elements of the list. The elements of the list are combined using the separator string, so it makes sense for the join() method to be a string method rather than a list method.
Recommended Course

Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.