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, where each element is identified by an index. In Python, we use lists to represent arrays and can convert them to strings with custom separators.

Arrays in Python

Python uses the list data structure to represent arrays. Here's an example of a list representing an array ?

numbers = [10, 4, 11, 76, 99]
print(numbers)
[10, 4, 11, 76, 99]

Input Output Scenarios

Let's see how array elements can be joined with specified characters ?

# Example 1: Join with 'a'
arr1 = [1, 5, 3, 6]
result1 = 'a'.join(map(str, arr1))
print(f"Input: {arr1}")
print(f"Output: {result1}")

print()

# Example 2: Join with 'P'
arr2 = [1, 2, 3, 10, 20]
result2 = 'P'.join(map(str, arr2))
print(f"Input: {arr2}")
print(f"Output: {result2}")
Input: [1, 5, 3, 6]
Output: 1a5a3a6

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

The join() Method

The join() method takes elements from an iterable and joins them using a string separator. It returns a string by joining all elements separated by the specified character.

Syntax

separator.join(iterable)

This method works with any iterable like lists, tuples, strings, dictionaries, or sets.

Using User-defined Function

We can create a custom function to convert array elements to strings before joining them ?

def toString(arr):
    result = []
    for item in arr:
        result.append(str(item))
    return result

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

specified_char = "a"
result = specified_char.join(toString(numbers))
print("The result is:", result)
The original array is: [1, 2, 3, 4, 5]
The result is: 1a2a3a4a5

Using map() Function

The map() function applies a function to every item in an iterable. We can use it to convert all elements to strings ?

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

specified_char = "a"
result = specified_char.join(map(str, numbers))
print("The result is:", result)
The original array is: [101, 102, 103, 104, 105]
The result is: 101a102a103a104a105

Using Lambda Expression

Lambda expressions create anonymous functions. We can use them to convert array elements to strings ?

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

specified_char = "a"
convert_to_str = lambda x: (str(i) for i in x)
result = specified_char.join(convert_to_str(numbers))
print("The result is:", result)
The original array is: [101, 102, 103, 104, 105]
The result is: 101a102a103a104a105

Working with Float Numbers

The same approach works with floating-point numbers ?

# Creating array with floats
decimals = [1.1, 1.2, 1.3, 1.4, 1.5]
print("The original array is:", decimals)

specified_char = "$"
convert_to_str = lambda x: (str(i) for i in x)
result = specified_char.join(convert_to_str(decimals))
print("The result is:", result)
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

Comparison of Methods

Method Code Length Readability Best For
User-defined function Long High Complex transformations
map() function Short High Simple type conversions
Lambda expression Medium Medium One-line transformations

Conclusion

The map(str, array) approach is the most concise and readable method for converting arrays to strings. Use join() with any character to create custom-separated string outputs from array elements.

Updated on: 2026-03-27T06:42:07+05:30

417 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements