Python string join method
Join string together
join
method does the opposite of split method.
It joins the elements in a sequence.
The join
method has the following syntax.
string.join(iterable)
Convert a list of characters back to a string
print ''.join(list('hello'))
li = ['a', 'b', 'e']
print "\n".join(li)
The code above generates the following result.
sep = '+' # from www . j av a2 s .com
seq = ['1', '2', '3', '4', '5']
print sep.join(seq) # Joining a list of strings
dirs = '', 'usr', 'bin', 'env'
print '/'.join(dirs)
print 'C:' + '\\'.join(dirs)
The code above generates the following result.
The sequence elements that are to be joined must all be strings.