OneBite.Dev - Coding blog in a bite size

convert an array to a string in python

Code snippet on how to convert an array to a string in python

arr = ['This','is','an','example','array'] 

def arrToString(arr):  
    return ' '.join(arr) 
  
print(arrToString(arr))

This code uses the join() method which is built in to Python to join the elements of a list together using a space as a separator. The first line creates an array with five elements. The second line creates a function that takes a list as an argument and returns a single string. The final line calls the function and passes it the array as an argument, and then prints the result. We can modify this code to use a different delimiter or to join the elements of a tuple or dictionary.

python