Python - String join() Method


Advertisements

The string join() method is used to return a string which contains all elements of an iterable separated by specified separator or string. For this method, all elements of the iterable must be string.

Syntax


string.join(iterable)

iterable Required. iterable object like list, tuple, set, string , dictionary etc with all string elements.

Example:Joining all elements of a list

In the below example, join() method to used to join all elements of the list taking whitespace as separator.

MySeparator = " "
MyList = ["John", "25", "London"]
print(MySeparator.join(MyList))

Output

John 25 London

Example: Joining all keys of a dictionary

In the below example, join() method to used to join all keys of the dictionary taking @ as separator.

MyDict = {
   "name": "John",
   "age": 25,
   "city": "London"
}
MySeparator = "@"
print(MySeparator.join(MyDict))

Output

name@age@city

String Methods
Advertisements