×

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?

By Sapna Deraje Radhakrishna Last updated : December 21, 2024

The string.join() Method

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 is it string.join(list) instead of list.join(string)?

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 Usage of string.join(list)

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

Output

1test2test3test4

Explanation

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

Example Usage of string.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)

Output

1test2test3test4

Comments and Discussions!

Load comments ↻





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