- How to convert string to list of characters in python
- 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
- Python str to list chars
- # 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
How to convert string to list of characters in python
This article will cover 4 different ways to split a string into a list of characters. One of these methods uses split() function while other methods convert the string into a list without split() function.
Method 1 : Using list constructor
Python list has a constructor which accepts an iterable as argument and returns a list whose elements are the elements of iterable.
An iterable is a structure that can be iterated. A string is also an iterable since it can be iterated over its characters.
Thus, a string passed to list constructor returns a list of characters of that string. Example,
str = 'codippa' l = list(str) print('List is:',l)
Method 2 : Using list comprehension
With list comprehension , an iterable can be easily converted to a list as shown below.
str = 'codippa' l = [c for c in str] print('List is:',l)
Method 3 : Using list slicing
A string can be converted to a list of characters using list slicing as shown below.
# initialize string str = 'codippa' # initialize list l = [] # assign string to list l[:] = str print('List is:',l)
where, l[:] is list slicing syntax.
In list slicing, start and end indexes are separated by a colon(:) in square brackets. Remember that if we do not provide any values for these, start index is set to 0(zero) and end index is set to the last element of list.
Thus, l[:] selects all elements of the list.
Method 4 : Using split() function
If the string contains a separator, then it can be converted into a list of characters with the separator removed.
Python’s inbuilt split() function accepts a string separator as argument and returns a list of characters with the separator removed. Example,
str = 'c-o-d-i-p-p-a' # split over - l = str.split('-') print('List is:', l)
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.
Python str to list chars
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.