Python Program to convert an array into a string and join elements with a specified character


An array is a data structure consisting of a collection of elements of the same data type, and each element is identified by an index.

 [2, 4, 0, 5, 8] 

Arrays in Python

Python does not have its own data structure to represent an array. Instead, we can use the list data structure to represent an array. Here, we will use list an array −

 [10, 4, 11, 76, 99]

In the article, we will write a python programs to convert an array to a string and join elements with a specified character.

Input Output Scenarios

Assume we have an input array with integer values. And in output will have string of all array elements are join with a specified character.

Input array:
[1, 5, 3, 6]
Output:
1a5a3a6

Here the array elements 1,5,3, 6 are joined with the character a. Let’s consider another array with integer values.

Input array:
[1, 2, 3, 10, 20]
Output:
1P2P3P10P20

Here the array elements 1, 2, 3, 10, 20 are joined with the character P. We will use an inbuilt string join() method to join array elements with a specified character.

Join() Method

The join() method takes elements from the iterable and joins them using a string. And returns a string by joining all the elements of an iterable, separated by the given string.

Syntax

string.join(iterable) 

This function takes an iterable (which can be List, Tuple, String, Dictionary, or Set).

User-defined Function

We will defined a user-define function using the def keyword in python to convert the array elements to a string. and join each element with a specified character.

Example

In this example we will define a toString function to convert array elements to strings. In the function, we will declare a empty list that will store the string converted elements. And by using the string.join() method we will join the array elements with the specified character.

def toString(L):
   r = []
   for i in L:
      r.append(str(i))
   return r

# creating array
arr = [1, 2, 3, 4, 5]
print ("The original array is: ", arr) 
print() 

specified_char = "a"
result = specified_char.join(toString(arr))
print ("The result is: ", result) 

Output

The original array is:  [1, 2, 3, 4, 5]

The result is:  1a2a3a4a5

We have successfully converted an array to a string and joined the elements with a specified character (“a”).

Using Map() Function

The map() function takes an iterable to applies the function on every item of the iterable. And returns an iterator that results from applied function.

Example

Here, we will use the map function to convert the array elements into string data type.

# creating array
arr = [101,102,103,104,105]

print ("The original array is: ", arr) 
print() 

specified_char = "a"
result = specified_char.join(list(map(str, arr)))
print ("The result is: ", result) 

Output

The original array is:  [101, 102, 103, 104, 105]

The result is:  101a102a103a104a105

In this example we have converted an array to a string and joined the elements with the character “a” by using the map() and join() functions.

Using Lambda

Lambda expressions are used to create anonymous functions, nothing but we can define an unnamed object for a function.

Example

In this example We will create an anonymous function using lambda to convert array elements to string. And then join the string-converted elements using join() method.

# creating array
arr = [101,102,103,104,105]
print ("The original array is: ", arr) 
print() 

specified_char = "a"
temp = lambda x : (str(i) for i in x)
result = specified_char.join(temp(arr))
print ("The result is: ", result) 

Output

The original array is:  [101, 102, 103, 104, 105]

The result is:  101a102a103a104a105

The variable temp holds the lambda expression which convets the data types of array elements to string data type.

Example

In this example, we will take another array with floating point numbers.

# creating array
arr = [1.1,1.2,1.3,1.4,1.5]
print ("The original array is: ", arr) 
print() 
 
specified_char = "$"
temp = lambda x : (str(i) for i in x)
result = specified_char.join(temp(arr))
print ("The result is: ", result) 

Output

The original array is:  [1.1, 1.2, 1.3, 1.4, 1.5]

The result is:  1.1$1.2$1.3$1.4$1.5

These are a few of the approaches to convert an array to a string and join elements with a specified character.

Updated on: 29-May-2023

152 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements