Join() Method Of String Class

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:

# 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:

draw me a sheep

id, Last Name, First Name, Height, Weight

 


Copyright 2019 © pythontic.com