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.


import_contacts

You may also like:

Related Posts

  • Remove Urls from String in Python
    03 May

    Remove Urls from Text in Python

    Table of ContentsIntroductionWays to remove URLs from Text in PythonUsing the re.sub() function to remove URLs from Text in PythonUsing the re.findall() function to remove URLs from Text in PythonUsing the re.search() function to remove URLs from Text in PythonUsing the urllib.urlparse class to remove URLs from Text in PythonConclusion In this post, we will […]

  • 16 February

    Remove Backslash from String in Python

    Table of ContentsHow to remove backslash from string in python?Using replace() Function to Remove Backslash from String in PythonUsing the decode() Function to Remove Backslash from String in PythonUsing re Library Functions to Remove Backslash from String in PythonUsing strip() Function to Remove Backslash from String in PythonHow To Remove Backslash from Json String in […]

  • Remove quotation marks from String in Python
    13 February

    Remove Quotation Marks from String in Python

    Table of ContentsQuotes in PythonRemove Double Quotes from String in PythonUsing the replace() Function to Remove Double Quotes from String in PythonUsing the re.sub() Function to Remove Double Quotes from String in PythonUsing the for Loop to Remove Double Quotes from String in PythonUsing the join() Function to Remove Double Quotes from String in PythonUsing […]

  • Parentheses from String in Python
    08 February

    Remove Parentheses from String in Python

    Table of ContentsWays to Remove Parentheses from String in PythonUsing the replace() Function to Remove Parentheses from String in PythonUsing the re.Sub() Function to Remove Parentheses from String in PythonUsing the pandas.Str.Replace() Function to Remove Parentheses from String in PythonConclusion In this article, we will see how to remove parentheses from String in Python. Parentheses […]

  • Remove word from String in Python
    08 February

    Remove Word from String in Python

    Table of ContentsRemove Word from String in PythonHow to Remove Word from Sentence in PythonUsing the replace() functionUsing the re.sub() functionUsing the startswith() functionUsing the removeprefix() functionUsing the endswith() functionUsing the removesuffix() functionHow to Remove Duplicate Words from String in PythonUsing the set() functionUsing the set() and join() functionsUsing the join() and a user-defined functionUsing […]

  • 22 January

    Replace space with underscore in Python

    Table of ContentsWays to replace space with underscore in PythonUsing the for loopUsing the replace() functionUsing the re.sub() functionUsing the split() and join() functionConclusion Strings are an essential data type in programming. In Python, we can treat strings as an iterable of characters, and can perform a variety of functions and operations on them. Replacing […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.