Python Program to convert the string into an array of character


In this article, we will learn how to develop a Python program using append(), extend(), and list() that will convert the given string into an array of characters. In this program, the array of characters represents the group of individual characters that break from the string.

Syntax

The following syntax is used in the examples −

append()

This is a predefined method used in Python that adds the character from the end.

extend()

This method is used to break the characters from a string.

list()

The method converts the string into a list of individual characters.

[*string_variable_name]

The symbol aestrick(*) used inside the list with the string variable will break the string and convert it into an array of characters.

Example 1

In the following example, we will start the program by storing the input string in the variable my_str. Then we create the empty list that will store the array of characters from a string. Next, start using for loop to iterate through each character(ch) in the string variable my_str. Now in the empty list, each and individual character is added to the list by using the predefined function append(). Finally, we are printing the result with the help of the variable arr_char.

my_str = "PSYCHOLOGY"
arr_char = []
for ch in my_str:
   arr_char.append(ch)
print("The string conversion into an array of characters:\n",arr_char)

Output

The string conversion into an array of characters:
['P', 'S', 'Y', 'C', 'H', 'O', 'L', 'O', 'G', 'Y']

Example 2

In the following example, we will start the program by storing the input string in the variable is_str. Then we create the empty string in the variable char_ar that will later store the array of characters. Then we use a predefined method extend() that accepts the parameter as a variable named is_str to convert the string into characters. Finally, we are printing the result with the help of the variable char_ar.

is_str = "The color is red"
char_ar = []
char_ar.extend( is_str )
print( "The string conversion into an array of characters:\n", char_ar )

Output

The string conversion into an array of characters:
 ['T', 'h', 'e', ' ', 'c', 'o', 'l', 'o', 'r', ' ', 'i', 's', ' ', 'r', 'e', 'd']

Note that, the empty space inside the string creates an array of characters.

Example 3

In the following example, we will start the program by storing the input string in the variable str_name. Then we initialize another variable named char_arr to store the string in the list(). The list method converts the string into a total number of characters. Finally, we are printing the variable with the help of the variable char_arr.

str_name = "COLOURFUL"
char_arr = list( str_name )
print( "The string conversion into an array of characters:\n", char_arr )

Output

The string conversion into an array of characters:
 ['C', 'O', 'L', 'O', 'U', 'R', 'F', 'U', 'L']

Example 4

In the following example, we will start the program by storing the input string in the variable my_str. Then the variable contains the aestrick(*) inside the list that will break the string letter into characters and get the result.

my_str = "Tutorialspoint"
print("The string conversion into an array of characters:\n",[*my_str])

Output

The string conversion into an array of characters:
 ['T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', 'p', 'o', 'i', 'n', 't']

Conclusion

The above outputs of four examples show the string conversion into an array of characters. There are four different methods used- list(), append(), extend(), and, [*var_name] and these methods formulate the result of an array of characters from the string.

Updated on: 01-Jun-2023

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements