Python String join method with Example – Python Tutorial

4 weeks ago codedestine 0

In this tutorial, we will learn about python string join method.
Python String Join

join method :

This method returns a string, which is a concatenation of all the strings in a iterable ( List, Tuple etc ) object. The separator between the elements of the iterable is a string object on which join method is called. An Exception ( Type Error ) will be raised, if iterable contains any non-string element.

Syntax : < String Object >.join(< Iterable Object >) :  < String Object >

Example:

# create list object and assign it to variable l l = ['a','b','c','d'] # create single space string and assign it to variable separator separator = ' ' # call join() method on separator by passing list l as an argument output = separator.join(l) # print the value of variable output print(output) # second example with '-' as a separator separator = '-' output = separator.join(l) print(output) # create tuple object and assign it to variable t t = ('Jan','Feb','Mar','Jan') # create single space string and assign it to variable separator separator = ' ' # call join() method on separator by passing tuple t as an argument output = separator.join(t) # print the value of variable output print(output) # second example with '-' as a separator separator = '-' output = separator.join(t) print(output)

References :-

  1. join() method Docs

That’s all for Python String join method. If you liked it, please share your thoughts in comments section and share it with others too.

Previous