In Python, strings are considered lists of characters, but even then they are not formatted and used in the same way as lists. Now, being a programmer requires you to be able to work with strings, characters, lists, and more. With that said, there are multiple ways in Python to convert a Python String into a list of characters. This post will list down all of the methods that can be used to achieve this feat.
Therefore, let’s begin with the first method which is to use the indexing brackets.
Method 1: Using Indexing “[ ]” Brackets
As mentioned above, strings are considered a list of characters. This means that the strings can be accessed using the string indexing brackets. Similarly, to lists, strings also have indexes starting from zero.
To use indexing brackets to turn a string into a list, you need to use the append() method. To demonstrate this, start by creating a string variable with a specific string value such as:
Now, you can access each individual character by using the index and place them inside a list by using the append method:
listVar.append(stringVar[0])
listVar.append(stringVar[6])
listVar.append(stringVar[9])
Lastly, simply call the print() function and print out the listVar onto the terminal:
When this program is executed, it prints the following content of the list “listVar” onto the terminal:
The output verifies that the string has been converted into a list of characters.
Method 2: Using the for-in Loop
The first method relies on the manual selection of characters to be placed inside the list, and if the user wants to convert every character of a string into a list then it is not an optimal approach. To achieve this feat, the user can utilize the for-in loop to iterate through each and every character of the string and append it into a list.
Let’s demonstrate this by first creating a string and an empty list using the following lines of code:
listVar = []
After that, simply use the for-in loop over the variable “stringVar” and append every character using the append() method:
listVar.append(item)
At the end, simply print out the entire list using the print() method:
When this code is executed, it produces the following output on the terminal:
The output verifies that the entire string has been converted into a list of characters.
Method 3: Using the list() Method
In Python, the list() method can be used to convert an entire string into a list object. To do this, the user has to simply call the list() method and pass the string as an argument. To illustrate this, take the following code snippet:
listVar = list(stringVar)
print(listVar)
When the above code is executed, it produces the following outcome on the terminal:
From the output, it is observable that the entire string has been converted into a list of characters using the list() method.
Method 4: Using the extend() Method
The extend() method is to extend a list and it is very similar to append(). The extend() method can take in a string as its input and each individual character of the string will be appended to the list. To demonstrate this, take the following code snippet:
listVar = []
listVar.extend(stringVar)
print(listVar)
When this above code is executed, it produces the following results:
The output confirms that the entire string has been converted into a list of characters.
Conclusion
To convert a string into a list of characters, the user can use the indexing brackets “[ ]” along with the append() method to add either individual characters or the entire string to the list. Apart from this, the user can utilize the built-in methods list() and extend() to convert an entire string into a list of characters. This post has elaborated on these methods in detail.