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?


import_contacts

You may also like:

Related Posts

  • 25 December

    Encode String to UTF-8 in Python

    Table of ContentsEncode String to UTF-8 in PythonUsing the encode() functionUsing the codecs.encode() functionConclusion The UTF-8 encoding is used by default in Python and represents 8-bit Unicode values. The upgrade to Python 3 saw a major change in using ASCII characters to Unicode characters by default for strings. Encode String to UTF-8 in Python This […]

  • 23 December

    How To Do Line Continuation in Python

    Table of ContentsUsing Backslash (\) OperatorLine Continuation in StringLine Continuation in Number ExpressionUsing Parentheses ()Line Continuation in StringLine Continuation in Number Expression Using Backslash (\) Operator We can use \ operator for line continuation in string and number expression as follows. Line Continuation in String To do line continuation in Python Strings: Use the backslash […]

  • 16 December

    Convert List to Comma Separated String in Python

    Table of ContentsUse .join() MethodUse .join() with map() MethodUse .join() with List Comprehension Use .join() Method To convert a list to a comma separated string in Python, use the .join() method. join() method takes all elements in an iterable and joins them into one string with delimiter as separator. [crayon-63b2b4bfe01b1507421747/] [crayon-63b2b4bfe01b4541420914/] First, we created a […]

  • 09 December

    Count Occurrences of Character in String in Python

    Table of ContentsUsing count() MethodUsing Naive for loopUsing List ComprehensionUsing re.findall() MethodUsing for loop with re.finditer() MethodUse the lambda Function with map()Use collections.Counter() Method Using count() Method Use String’s count() method to count occurrences of character in String in Python e.g. my_string.count(character). count() method returns number of occurrences of the character in String. [crayon-63b2b4bfe02b1343025173/] We […]

  • 08 December

    Find Character in String in Python

    Table of ContentsUsing find() MethodUsing index() MethodUsing for Loop with re.finditer() MethodUsing list comprehension Using find() Method To find the character in a string in Python: Use the find() method to find the index of the first occurrence of the supplied character in the input String. Use an if statement to check if the returned […]

  • 05 December

    Remove Substring from String in Python

    Table of ContentsUse replace() MethodUse the split() Method with for loopUse split() with join() MethodUse re.sub() Method Use replace() Method To eliminate a substring from string in Python: Define and initiate my_string. Declare substring to remove it from my_string. Use replace() method to remove substring from string. e.g. my_string = my_string.replace(substring, "") [crayon-63b2b4bfe0a76334632412/] The code […]

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.