- Python str to char list
- # Table of Contents
- # Split a String into a List of Characters in Python
- # Split a String into a list of characters using a list comprehension
- # Split a String into a list of Characters using a for loop
- # Split a String into a List of Characters using iterable unpacking
- # Split a String into a List of Characters using extend
- # Split a String into a List of Characters using map()
- # Additional Resources
- Python String to List of Characters
- Method 1: Using Indexing “[ ]” Brackets
- Method 2: Using the for-in Loop
- Method 3: Using the list() Method
- Method 4: Using the extend() Method
- Conclusion
- About the author
- Abdul Mannan
- Split String to Char Array in Python
- Use the for Loop to Split a String Into Char Array in Python
- Use the list() Function to Split a String Into Char Array in Python
- Use the extend() Function to Split a String Into Char Array in Python
- Use the unpack Method to Split a String Into Char Array in Python
- Use the List Comprehension Method to Split a String Into Char Array in Python
- Related Article — Python String
Python str to char list
Last updated: Feb 19, 2023
Reading time · 4 min
# Table of Contents
# Split a String into a List of Characters in Python
Use the list() class to split a string into a list of characters, e.g. my_list = list(my_str) .
The list() class will convert the string into a list of characters.
Copied!my_str = 'bobby' my_list = list(my_str) # 👇️ ['b', 'o', 'b', 'b', 'y'] print(my_list)
The list class takes an iterable and returns a list object.
When a string is passed to the class, it splits the string on each character and returns a list containing the characters.
# Split a String into a list of characters using a list comprehension
Copied!my_str = 'bobby' my_list = [letter for letter in my_str] # 👇️ ['b', 'o', 'b', 'b', 'y'] print(my_list)
List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.
You can also filter letters out of the final list when using this approach.
Copied!my_str = 'b o b b y' my_list = [letter for letter in my_str if letter.strip()] # 👇️ ['b', 'o', 'b', 'b', 'y'] print(my_list)
The string in the example has spaces.
Instead of getting list items that contain a space, we call the strip() method on each letter and see if the result is truthy.
The str.strip method returns a copy of the string with the leading and trailing whitespace removed.
If the string stores a space, it would get excluded from the final list.
# Split a String into a list of Characters using a for loop
You can also use a simple for loop to split a string into a list of characters.
Copied!my_str = 'bobby' my_list = [] for letter in my_str: my_list.append(letter) # 👇️ ['b', 'o', 'b', 'b', 'y'] print(my_list)
We used a for loop to iterate over the string and use the append method to add each letter to the list.
The list.append() method adds an item to the end of the list.
The method returns None as it mutates the original list.
You can also conditionally add the letter to the list.
Copied!my_str = 'bobby' my_list = [] for letter in my_str: if letter.strip() != '': my_list.append(letter) # 👇️ ['b', 'o', 'b', 'b', 'y'] print(my_list)
The string is only added to the list if it isn’t a space.
# Split a String into a List of Characters using iterable unpacking
You can also use the iterable unpacking * operator to split a string into a list of characters.
Copied!my_str = 'bobby' my_list = [*my_str] print(my_list) # 👉️ ['b', 'o', 'b', 'b', 'y']
Notice that we wrapped the string in a list before using iterable unpacking.
The * iterable unpacking operator enables us to unpack an iterable in function calls, in comprehensions and in generator expressions.
Copied!example = (*(1, 2), 3) # 👇️ (1, 2, 3) print(example)
# Split a String into a List of Characters using extend
You can also use the list.extend() method to split a string into a list of characters.
Copied!my_str = 'bobby' my_list = [] my_list.extend(my_str) print(my_list) # 👉️ ['b', 'o', 'b', 'b', 'y']
The list.extend method takes an iterable and extends the list by appending all of the items from the iterable.
Copied!my_list = ['bobby'] my_list.extend(['hadz', '.', 'com']) print(my_list) # 👉️ ['bobby', 'hadz', '.', 'com']
The list.extend method returns None as it mutates the original list.
We can directly pass a string to the list.extend() method because strings are iterable.
Each character of the string gets added as a separate element to the list.
# Split a String into a List of Characters using map()
You can also use the map() function to split a string into a list of characters.
Copied!my_str = 'bobby' my_list = list(map(lambda char: char, my_str)) print(my_list) # 👉️ ['b', 'o', 'b', 'b', 'y']
Instead of passing the string directly to the list() class, we used the map() function to get a map object containing the characters of the string.
The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.
The lambda function we passed to map gets called with each character of the string and returns it.
The last step is to convert the map() object to a list.
# Additional Resources
You can learn more about the related topics by checking out the following tutorials:
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
Python String to List of Characters
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:
After that, simply use the for-in loop over the variable “stringVar” and append every character using the append() method:
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:
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:
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.
About the author
Abdul Mannan
I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor’s degree in computer science.
Split String to Char Array in Python
- Use the for Loop to Split a String Into Char Array in Python
- Use the list() Function to Split a String Into Char Array in Python
- Use the extend() Function to Split a String Into Char Array in Python
- Use the unpack Method to Split a String Into Char Array in Python
- Use the List Comprehension Method to Split a String Into Char Array in Python
This tutorial, we learn how to split a string into a list of characters in Python.
Use the for Loop to Split a String Into Char Array in Python
In this method, we use the for loop to iterate over the string and append each character to an empty list. See the following example code.
word = 'Sample' lst = [] for i in word: lst.append(i) print(lst)
Use the list() Function to Split a String Into Char Array in Python
Typecasting refers to the process of converting a datatype to some other datatype. We can typecast a string to a list using the list() function which splits the string to a char array. For example,
word = 'Sample' lst = list(word) print(lst)
Use the extend() Function to Split a String Into Char Array in Python
The extend() function adds elements from an iterable object like a list, tuple, and more to the end of a given list. Refer to this article to know more about the difference between the extend() and append() functions.
Since a string is a collection of characters, we can use it with the extend() function to store each character at the end of a list. For example,
lst = [] word = 'Sample' lst.extend(word) print(lst)
Use the unpack Method to Split a String Into Char Array in Python
The * operator can be used to perform unpacking operations on objects in Python. This method unpacks a string and stores its characters in a list, as shown below.
Use the List Comprehension Method to Split a String Into Char Array in Python
List Comprehension is an elegant way of creating lists in a single line of code. In the method shown below, we use the for loop to iterate over the list and store each element.
word = "Sample" lst = [x for x in word] print(lst)
Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.