Home  >  Article  >  Backend Development  >  Python program to convert character array to string

Python program to convert character array to string

WBOY
WBOYforward
2023-08-19 09:25:133302browse

Python program to convert character array to string

In Python, we have some built-in functions such as join(), map(), reduce(), and len(), which can be used to convert character arrays into strings. Character arrays are defined by breaking each character out of a string. Python has a predefined method join() that can join all characters to form a string. Another way to solve this problem is to use list comprehensions and the join() method.

Let us take an example to understand the process of converting character array to string -

    The
  • characters are 'p', 'e' and 'n': the string becomes pen.

  • Characters are 'S', 'A', 'N', 'D', 'B', 'O', 'X': the string becomes SANDBOX.

grammar

join()

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

len()

len() is a built-in method in Python that is used to determine the total number of lengths.

map()

You can use Python's map() method to apply a function to each item in an iterable object (such as a list or tuple), and then return a new iterable object containing the result.

reduce()

This is a built-in function in Python that follows the module called functools and performs specific operations on a list of elements.

The Chinese translation of

Example 1

is:

Example 1

In this program, we will start the program by storing the entered list characters in the variable arr_char. The variable 'changer' is then initialized by defining an empty string using the join() method, which creates the formation of the string. Finally, print the results.

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
The Chinese translation of

Example 2

is:

Example 2

In the following example, we will start the program by storing an input list of character arrays in the variable ‘ary_char’. Create an empty string variable 'c_str' which will later store all characters into the string. Then use a for loop to iterate the variable 'ary_char' into the variable 'char', adding all the characters together. Finally, print the variable with the help of 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
The Chinese translation of

Example 3

is:

Example 3

In the example below, the while loop in this code converts the character array into a string. Iterates over each character in the array and adds the character to the initially empty string. When the loop completes, print out the resulting string.

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
The Chinese translation of

Example 4

is:

Example 4

In the following example, the code uses the map function and the join method of string to convert the character array into a string. It first constructs a character array and then uses the map function to apply the str function to each element of the array. The iterable of strings is then sent to the join method, which concatenates all the strings together, creating a single string. Then, print the resulting string.

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
The Chinese translation of

Example 5

is:

Example 5

pip install funtools

Install the above commands into your Python system

In the example below, the function char_to_str is defined in this code, which accepts a list of characters as input and outputs a string. Each character will be added to an initially empty string emp_str as the function iterates over the elements of list s. It then outputs a string concatenated from the character arrays.

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

Please note that the functools module in Python provides tools for interacting with other functions as well as higher-order functions. Functions that can accept other functions as arguments or return them as results are called higher-order functions. This module provides several functions such as reduce(), chache() and partial().

Example 6

is translated as:

Example 6

In the following example, you start the program by importing a module named functools, which provides a built-in method named reduce(). Then define a function called char_to_str that takes as input a list of characters s and returns a string. This function uses the reduce() function of the functools module to apply a lambda function to each element of the list s from left to right. The lambda function joins two inputs x and y. By doing this, the character lists are combined into a single value that represents the entire character list.

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

in conclusion

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

The above is the detailed content of Python program to convert character array to string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete