Is their any functions in python to join a list of strings to a single string ?

we know that function has rich set of built in functions in python, so the answer is off course their is a functions in python to join a list of strings to a single string .as the name suggest join() is the function in python to join a list of strings to a single string.Let we check the syntax of join().

string.join(list)

where string is the separator string and list is a list of strings

Example of join function in python

item= [‘ Hello’, ‘good morning ‘]

string = ‘,’.join(item)

print(string)

any functions in python to join a list of strings to a single string

Is their any functions in python to split a string in to a list of strings ?

Fortunately we have split() built-in function in python to split a string in to a list of strings .we can define any separator, anyway default separator is any whitespace. so by default spilt() function split a string in to a list of strings based on any whitespace.

Syntax of split() function in Python

string.split(separator, maxsplit)

separator : It is an optional parameter, if we specify based on the separator value split a string in to a list of strings
maxsplit: It is also optional parameter, it specify how many number of splits to , by default it is all the occurrence.

Usage of split() function in Python

Hello_string = ” Hello, good morning ”

myitem = Hello_string.split(“, “)

print(myitem)

functions in python to split a string in to a list of strings

What is copy module in python

Like other programming language python does not support = operation to copy an objects,instead it creates a new variable that shares the reference of the original object.Fortunately we have copy module in python copy an object to a variable.Their are two ways to use copy module in python.They are Shallow Copy and Deep Copy .

Deep copy in python

Deep copy is one of the way copy module in python,in Deep copy process a copy of object is copied in other object.so whatever the change we make on the Deep copy object it will not reflect in the original object.Deep copy copies all values recursively from source to target object. First constructing a new collection object and then recursively populating it with copies of the child objects found in the original

Deep copy example

import copy
  
# initializing item1 
item1 = [7, 12, [9,50], 14]
  
# using deep copy to copy item1 
item2 = copy.deepcopy(item1 )
  
# original elements of item1 
print ("Orignal item1 ")
for i in range(0,len(item1)):
    print (item1[i],end=" ")
  
# adding and element to item2 
item2[2][0] = 69

print("\r")

print ("Deep copy result item2")
for i in range(0,len( item2 )):
    print (item2[i],end=" ")
  
print("\r")

print ("After copying Deep copy Orignal item1 ")
for i in range(0,len( item1)):
    print (item1[i],end=" ")


Copy-module-in-Pytho-deep-copy

shallow copy example

shallow copy is a way in which a reference of object is copied in other object. so any changes made to a shallow copy of object do reflect in the original object.shallow copy is done with “copy()” function.

import copy
  
# initializing item1 
item1 = [7, 12, [9,50], 14]
  
# using deep copy to copy item1 
item2 = copy.copy(item1 )
  
# original elements of item1 
print ("Orignal item1 ")
for i in range(0,len(item1)):
    print (item1[i],end=" ")
  
# adding and element to item2 
item2[2][0] = 69

print("\r")

print ("Shallow copy result item2")
for i in range(0,len( item2 )):
    print (item2[i],end=" ")
  
print("\r")

print ("After copying Shallow copy Orignal item1 ")
for i in range(0,len( item1)):
    print (item1[i],end=" ")

Shallow copy module in Python