Implode (join) and explode (split) in python

Implode and explode is the basic operation in any programming language. Here i will show you how to join and array or List items in single string or split sting into multiple variables or array in python.

Sometimes you may need to break a large string down into smaller parts or strings.  In python split is the fuction that is used to break string into multiple parts based on a seperator. 

If you will not use an seperator then python uses space to split string.

Lets have an example without seperator:

str = "This is my lovely city"
list = str.split()
print(list)


Output:
['This', 'is', 'my', 'lovely', 'city']

Now take another example with seperator:

str = "Orange,Banana,Grapes"
list = str.split(',')
print(list)

Output:
['Orange', 'Banana', 'Grapes']

Now lets extract values in multiple variable in string format:

str = "Orange,Banana,Grapes"
a,b,c = str.split(',')
print(a)
print(b)
print(c)

Output:

Orange
Banana
Grapes

Now you will see how implode or join function works:

list = ["Orange","Banana","Grapes"]
str = ", ".join(list)
print(str)

Output:

Orange, Banana, Grapes

So here you can see to join a list methot is reversed. Here join function is applied on string seperator, not on list. List is passed as parameter.

 

 


Keywords: