{article Dive into Python}{title} {text}{/article}

You have a list of key−value pairs in the form key=value, and you want to join them into a single string. To join any list of strings into a single string, use the join method of a string object.

The join method joins the elements of the list into a single string, with each element separated by a semi−colon. The delimiter doesn't need to be a semi−colon; it doesn't even need to be a single character. It can be any string.

join works only on lists of strings; it does not do any type coercion. Joining a list that has one or more non−string elements will raise an exception.

Example

Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> s = "Joining Lists and Splitting String"
>>> s.split()
['Joining', 'Lists', 'and', 'Splitting', 'String']
>>> 'Joining Lists and Splitting String'.split()
['Joining', 'Lists', 'and', 'Splitting', 'String']
>>> s.split ('i')
['Jo', 'n', 'ng L', 'sts and Spl', 'tt', 'ng Str', 'ng']
>>> string = s.split()
>>> string
['Joining', 'Lists', 'and', 'Splitting', 'String']
>>> for s in string: print(s)

Joining
Lists
and
Splitting
String
>>> new = ':'.join(string)
>>> new
'Joining:Lists:and:Splitting:String'
>>> ', '.join(string)
'Joining, Lists, and, Splitting, String'
>>>