×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

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

Last Updated : April 27, 2025

In Python, string.join(list) is used because the string defines the separator, making it responsible for joining the list elements together.

The join() is a string method and while using it the separator string iterates over an arbitrary sequence, forming string representations of each of the elements, inserting itself between the elements.

Why join() Is a String Method and Not a List Method in Python

Concisely, it's because join and string.join() are both generic operations that work on any iterable and is a string method.

Because it is a string method, the join() can work for the Unicode string as well as plain ASCII strings.

Example 1: Joining List Elements with a String Separator

test_string = "test"
result = test_string.join(["1", "2", "3", "4"])
print(result)

The output of the above code will be:

1test2test3test4

Explanation

In the above example, the string "test" is joined between every element of the list ["1", "2", "3", "4"].

Example 2: Using join() with a String

# Define the string
test_string = "test"

# Use the join method with a string
result = test_string.join("---")

# Print the result
print(result)

The output of the above code will be:

-tteste-ttest-

Exercise

Select the correct option to complete each statement about why Python uses string.join(list) instead of list.join(string).

  1. The join() method is called on the ___ that will appear between list elements.
  2. In Python, lists do not have a ___ method because the join operation conceptually belongs to strings.
  3. The join() method returns a single ___ after joining all list elements.

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.