Convert String to Char array in Python

String to char Array in Python

Strings are made up of characters. In python, we don’t have a character or char data type. All the characters are recognized as strings of length one. In this article, we will discuss different approaches to convert a string to a char array in python. We will also implement the programs to convert a string to a char array in python.

Convert String To Char Array In Python Using For Loop

The first approach for converting a string to a char array that we are going to discuss uses a for loop and the append() method defined for the list. In this approach, we will first create an empty list. After that, we will iterate through the input string using a for loop. During iteration, we will add each character of the string to the list using the append() method.

The append() method, when invoked on a list, accepts the character as input and appends it to the list that we have created. After execution of the for loop, we get an array of characters as seen in the following example.

Output:

Convert String To Char Array In Python Using The list() Constructor

Instead of using a for loop and the append() method, we can directly use the list() constructor to convert string to char array in python. The list() constructor accepts an iterable object as an input argument and returns a list of all the elements of the iterable object. 

As a string is an iterable object, to convert a string to a char array, we will pass the string to the list() constructor and it will return a list of all the characters in the list as follows.

Output:

Convert String To Char Array In Python Using The extend() Method

Another approach to convert a string into a char array is by using the extend() method. The extend() method, when invoked on a list, accepts an iterable object as its input argument and appends all the elements of the iterable object to the list. As a string object is an iterable object, we can pass the string object as an input argument to the extend() method.

To convert a string to a char array, we will first define an empty list. After that, we will use the extend() method to append all the characters of the input string to the list. In this way, we can obtain a list of characters from a string as shown in the following example.

Output:

Convert String To Char Array In Python Using The += Operator

The += operator also works in a similar way to the extend function. The syntax for using the += operator with a list is as follows.

myList+=iterable_object.

Here, myList is the existing list to which elements of the iterable_object have to be added.

To convert a string to a char array using the += operator, we will first create an empty list. After that, we will use the += operator to add the characters of the string to the list as follows.

Output:

Convert String To Char Array In Python Using Packing And Unpacking

In python, * is used as the unpacking operator. Whenever we want to break a container object or an iterable object into its elements, we use the unpacking operator as follows.

After unpacking the iterable object, we can perform the packing operation to create any container object such as a list, tuple, or dictionary. 

To convert a string to a char array using the * operator, we will first unpack the string. After that, we will pack the unpacked characters into a list as shown below.

Output:

Convert String To Char Array In Python Using List Comprehension

We can also use list comprehension to convert a string to a char array. The syntax for list comprehension is as follows.

newList=[expression for element in iterable]

Here,

  • newList is the list that we want to create.
  • iterable’  is the iterable object whose ‘element’ is to be included in newList.
  • expression’ is any mathematical or logical expression of the ‘element’.

To convert a string to a char array using list comprehension, we will pass the string in the place of iterable. The ‘element’ will refer to each character in the string. The expression will be the same as the element because we don’t need to change the characters. You can observe this in the following example.

Output:

Conclusion

In this article, we have discussed six ways to convert a string to a char array in python. For performance reasons, I would suggest you use list() constructor or packing and unpacking for this operation. All other operations are costlier in terms of time and space compared to these two methods. 

I hope you enjoyed reading this article.

Happy Learning.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *