The String join()
method combines all of the items in an iterable into a single string with each item joined by the string separator.
string.join(iterable)
The join()
method requires an iterable object where all the
returned values are strings.
The join()
method returns a string concatenated with the elements of the iterable.
The following code shows how to use the String join()
method:
A program that joins all items in a dictionary into a string, using "-"
as the separator.
# Creating a dictionarymyDict = {"name": "Maria", "id": "1128de0012", "gender": "Female"}# Using "-" as seperatormySeparator = "-"new_dict = mySeparator.join(myDict)print(new_dict)
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 printprint(mySeperator.join(myList))
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 separatorprint("".join(myList))