Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
string

What is the String join() method in Python?

Maria Elijah

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The String join() method combines all of the items in an iterable into a single string with each item joined by the string separator.

Syntax


string.join(iterable)

Parameter

The join() method requires an iterable object where all the returned values are strings.

Return value

The join() method returns a string concatenated with the elements of the iterable.

Code

The following code shows how to use the String join() method:

Example 1

A program that joins all items in a dictionary into a string, using "-" as the separator.

# Creating a dictionary
myDict = {"name": "Maria", "id": "1128de0012", "gender": "Female"}

# Using "-" as seperator
mySeparator = "-"

new_dict = mySeparator.join(myDict)

print(new_dict)

Example 2

A program that joins all items in a list into a string, using "OF" as the separator.

myList = ['1','2','3','4'] 
  
mySeperator = "OF"
  
# joins elements of myList by 'OF'
# and print
print(mySeperator.join(myList))

Example 3

A program that joins with an empty string.

# using of join() to join a list
# without any separator.
  
myList = ['E','d','u','c', 'a', 't', 'i', 'v', 'e'] 
# Joining with empty separator
print("".join(myList))

RELATED TAGS

python
string

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring