Python Program to convert the array of characters into the string


In Python, we have some built-in functions like join(), map(), reduce(), and len() that can be used to convert the array of characters into the string. The array of characters is defined by breaking each character from the string. Python has a predefined method join() to join all the characters to make the string. Another way to solve the program is by using list comprehension and the join() method.

Let’s take an example to understand the conversion of an array of characters into a string −

  • The character is ‘p’, ‘e’, and ‘n’: The string becomes a pen.

  • The character is ‘S’, ‘A’, ‘N’, ‘D’, ‘B’, ‘O’, ‘X’: The string becomes SANDBOX.

Syntax

join()

The join() is a predefined method in Python to join all characters together to form the string.

len()

The len() is a built-in method in Python that can be used for determining the total number of length.

map()

A function can be applied to each item in an iterable (such as a list or tuple) using Python's map() method, which then returns a new iterable with the results.

reduce()

This is a built-in function in Python that follows the module named functools and carries out a specific operation on a list of elements.

Example 1

In this program, we will start the program by storing the input list characters in the variable arr_char. Then initialize the variable ‘changer’ to store the string by defining the empty string with the join() method that creates the formation of the string. Finally, print the result.

arr_char=['T','u', 't', 'o', 'r','i', 'a', 'l','s', 'p', 'o', 'i', 'n', 't']
changer=''.join(arr_char)
print("The string is", changer)

Output

The string is Tutorialspoint

Example 2

In the following example, we will start the program by storing the input list of an array of characters in the variable ‘ary_char’. Make the empty string variable ‘c_str’ which will later store the all characters into a string. Then use the for loop to iterate the variable ‘ary_char’ into variable ‘char’ to add the all character together. Finally, print the variable with the help of the variable ‘c_str’.

#convert character array into a string
ary_char = ['P','E','N','C','I','L']
c_str = ""
for char in ary_char:
 c_str += char
print("The string is",c_str)

Output

The string is PENCIL

Example 3

In the following example, the while loop in this code turns an array of characters into a string. Each character in the array is iterated over, and then the character is added to an originally empty string. The outcome string is printed when the loop is finished.

char_arr = ['B', 'l', 'a', 'c', 'k']
emp_str = ""
i = 0
while i < len(char_arr):
   emp_str += char_arr[i]
   i += 1
print(emp_str)

Output

Black

Example 4

ln the following example, the map function and the join method of strings are used in this code to turn an array of characters into a string. It first constructs a character array, after which it applies the str function to each element of the array using the map function. After that, the iterable of strings is sent to the join method, which joins all the strings together to create a single string. After that, the resultant string is printed.

char_arr = ['S', 'c', 'h', 'o', 'l','a','r']
my_str = ''.join(map(str, char_arr))
print("The array of character into string:", my_str)

Output

The array of character into string: Scholar

Example 5

pip install funtools

Install the above command to your Python system

In the following example, the function char_to_str is defined in this code that accepts a list of characters as input and outputs a string. Each character will be added to an initially empty string called emp_str as the function iterates through the elements of the list s. It then outputs the string that was merged from an array of characters.

import functools
def char_to_str(s):
   emp_str = ""
   for x in s:
      emp_str += x
   return emp_str
arr_char = ['T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', 'p', 'o', 'i', 'n','t']
print(char_to_str(arr_char))

Output

Tutorialspoint

Note that, the functools module in Python offers tools for interacting with other function as well as higher-order functions. The function that can take other functions as arguments or return them as results are known as higher-order functions. This module offers several functions, such as reduce(), chache(), and partial().

Example 6

In the following example, begin the program by importing the module named functools that provides the built-in method named reduce(). Then define a function char_to_str that takes a list of characters s as input and returns a string. The function applies a lambda function together from left to right to each element of the list s using the reduce() function from the functools module. The lambda function joins the two inputs, x, and y. By doing this, the list of characters is combined into a single value representing the entire list of characters.

import functools
def char_to_str(s):
   return functools.reduce(lambda x, y: x + y, s)
arr_char = ['T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', 'p', 'o', 'i', 'n','t']
print(char_to_str(arr_char))

Output

Tutorialspoint

Conclusion

We understood the difference between the two examples by converting the array of characters into a string. In Example 1, the predefined method join() is used to join all characters together whereas in Example 2, the for loop is used to iterate each character and store to empty variable to print the string.

Updated on: 01-Jun-2023

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements