Table of Contents
- Convert String To Char Array In Python Using For Loop
- Convert String To Char Array In Python Using The list() Constructor
- Convert String To Char Array In Python Using The extend() Method
- Convert String To Char Array In Python Using The += Operator
- Convert String To Char Array In Python Using Packing And Unpacking
- Convert String To Char Array In Python Using List Comprehension
- Conclusion
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.
1 2 3 4 5 6 7 8 |
myString = "Java2Blog" listOfChars = list() for character in myString: listOfChars.append(character) print("The input string is:", myString) print("The list of character is:", listOfChars) |
Output:
1 2 3 4 |
The input string is: Java2Blog The list of character is: ['J', 'a', 'v', 'a', '2', 'B', 'l', 'o', 'g'] |
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.
1 2 3 4 5 6 |
myString = "Java2Blog" listOfChars = list(myString) print("The input string is:", myString) print("The list of character is:", listOfChars) |
Output:
1 2 3 4 |
The input string is: Java2Blog The list of character is: ['J', 'a', 'v', 'a', '2', 'B', 'l', 'o', 'g'] |
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.
1 2 3 4 5 6 7 |
myString = "Java2Blog" listOfChars = list() listOfChars.extend(myString) print("The input string is:", myString) print("The list of character is:", listOfChars) |
Output:
1 2 3 4 |
The input string is: Java2Blog The list of character is: ['J', 'a', 'v', 'a', '2', 'B', 'l', 'o', 'g'] |
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.
1 2 3 4 5 6 7 |
myString = "Java2Blog" listOfChars = list() listOfChars += myString print("The input string is:", myString) print("The list of character is:", listOfChars) |
Output:
1 2 3 4 |
The input string is: Java2Blog The list of character is: ['J', 'a', 'v', 'a', '2', 'B', 'l', 'o', 'g'] |
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.
1 2 3 |
*objectName |
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.
1 2 3 4 5 6 |
myString = "Java2Blog" listOfChars = [*myString] print("The input string is:", myString) print("The list of character is:", listOfChars) |
Output:
1 2 3 4 |
The input string is: Java2Blog The list of character is: ['J', 'a', 'v', 'a', '2', 'B', 'l', 'o', 'g'] |
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 innewList
. - ‘
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.
1 2 3 4 5 6 |
myString = "Java2Blog" listOfChars = [character for character in myString] print("The input string is:", myString) print("The list of character is:", listOfChars) |
Output:
1 2 3 4 |
The input string is: Java2Blog The list of character is: ['J', 'a', 'v', 'a', '2', 'B', 'l', 'o', 'g'] |
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.