join() method of string class

join() method of stri class takes multiple strings of an iterable and concatenates them using the source string as sepearator and returns the new string.

Str Updated 12 Mar 2017 1 min read

Method Name

join()

Method Signature

join(iterable)

Method Overview

  • join() method takes any python iterable object of string elements as its parameter and joins all of them with the source string as the separator returning the new string object.

  • As strings in Python being immutable sequences join() is used frequently to obtain new strings composed from multiple strings.

Example

python
# tuple of strings

words       = ("draw", "me", "a", "sheep")

sentence    = " ".join(words)

print(sentence)

# Form comma separated values header using join

csvFields       = ("id", "Last Name", "First Name", "Height", "Weight")

csvHeader       = ", ".join(csvFields)

print(csvHeader)

Output

Output
draw me a sheep

id, Last Name, First Name, Height, Weight