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.

Related Posts

  • Split String with multiple delimiters
    17 November

    Split String With Multiple Delimiters In Python

    Table of ContentsSplitting a string in PythonWays to split a string in PythonUsing the split() and replace() functionsUsing the regular expressions In this post, we will see how to split String with multiple delimiters in Python. Splitting a string in Python When we talk about splitting a string, it means creating a collection of sub-strings […]

  • Loop through String in Python
    28 October

    Loop through String in Python

    Table of ContentsUse the for loop to loop through String in PythonUsing the range() functionUsing the slicing [] operatorUsing the enumerate() functionUse the while loop to loop through String in PythonConclusion In this article, we will see how to loop through String in Python. Iteration or looping means executing a set of statements till a […]

  • Get variable name as String in Python
    01 October

    How to get variable name as String in Python

    Table of ContentsWhat are variable names in Python?How to get a variable name as a string in Python?In Python 2.xIn Python 3.x In Python, there are several ways to convert a string to a variable name, but the reverse process is not exactly utilized so often in the programming world. This article focuses on how […]

  • Python hex to bytes
    31 May

    Convert Hex to bytes in Python

    Table of ContentsUse codecs.decode() function to Convert Hexadecimal value to Byte in PythonUse bytes.fromhex() function to Convert Hexadecimal value to Byte in PythonUse the binascii Module to Convert Hexadecimal value to Byte in Python 💡 Outline You can use codecs’s decode() function to convert Hex to bytes in python. This method works for Python 2.x […]

  • Remove commas from String in Python
    23 May

    Remove comma from String in Python

    Table of ContentsUsing replace() function to remove comma from string in PythonUsing re or RegEx package function to remove comma from string in Python 💡 Outline You can remove comma from string in python by using string’sreplace() method. Syntax: [crayon-61a0cc249c7c7926656736/] In this tutorial, we will take a sample string with a couple of commas in […]

  • Add character to String in Python
    21 May

    Add character to String in Python

    Table of ContentsUsing the + operatorUsing the join() functionUsing the % operatorUsing the format() functionUsing the f-strings Strings are considered as a sequence of characters in programming. In Python, string objects are considered immutable. What this means is that string objects cannot be changed once created, and a new string is created with a different […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.